How to change Laravel model's table name

How to change Laravel model's table name

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

How to Change Laravel Model's Table Name

Laravel relies heavily on a convention-over-configuration approach, which means the framework automatically guesses your database table name based on the pluralized version of your model class name. If you are using the Eloquent ORM without explicit configuration, changing the physical name of a database table in your schema does not automatically update how Laravel queries that data. You will encounter an error like SQLSTATE[42S02]: Base table or view not found because the framework is still trying to hit domain_related_settings while your model expects it to query DomainRelatedSettings.

This mismatch happens because Eloquent generates the SQL query using a default mapping derived from your PHP class naming convention. When you roll back migrations and rename the table in the database, you have only updated the schema layer. The application layer—the Eloquent model—still holds onto the original guessed table name unless you explicitly tell it otherwise.

To resolve this, you need to override the default table mapping for your specific model. This is done by defining the getTable() method within your model class. Inside this method, you simply return the exact string of the new database table name. For a model named DomainRelatedSetting, the implementation would look like this:

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class DomainRelatedSetting extends Model
{
    protected $fillable = ['hostname', 'setting_value']; // Ensure these match your schema

    /**
     * Specify the database table name.
     *
     * @return string
     */
    public function getTable()
    {
        return 'DomainRelatedSettings';
    }
}

By implementing this method, you decouple the model's identity from Laravel's automatic naming convention. The framework will now execute queries against DomainRelatedSettings instead of its pluralized guess, aligning with your renamed database table. This approach is robust because it persists across different environments and does not require changes to every controller or repository that interacts with this data.

If you prefer a more concise syntax, Laravel allows you to define the table name using the $table property on the model class itself. Setting this property achieves the same result as the getTable() method but offers a shorter declaration:

class DomainRelatedSetting extends Model
{
    protected $table = 'DomainRelatedSettings';
    
    protected $fillable = ['hostname', 'setting_value'];
}

When using the $table property, ensure you do not include quotes around the table name in the PHP string. The SQL generator handles adding backticks or quotes when constructing the final query. It is also important to verify that your model's fillable attributes match the actual column names in your renamed table to prevent mass assignment errors.

While renaming tables and adjusting models works perfectly for isolated changes, you must be cautious if this change affects other parts of your application. If other models or migration files reference this table by name or assume a specific schema structure, those dependencies could break. Always check related migrations that depend on the foreign keys or column definitions associated with DomainRelatedSetting.

For more complex scenarios involving multiple tables or intricate relationships, Laravel's official documentation provides extensive guidance on customizing model attributes and managing database connections. You can find detailed examples at https://laravelcompany.com regarding how to manage database schemas effectively in your projects.

Our network