Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel model looks to old table name

I am using Laravel 5 and have changed the name of a database table from "domain_related_settings" to "DomainRelatedSettings" by rolling back all migrations, changing the specific migration, and running them again. The new table name is reflected in the database.

But when i use the corresponding model DomainRelatedSetting in a statement like this:

$domainSettings = DomainRelatedSetting::where('hostname', 'foo')->first();

it gives the following error:

SQLSTATE[42S02]: Base table or view not found:
1146 Table 'databasename.domain_related_settings' doesn't exist
(SQL: select * from `domain_related_settings` where `hostname` = foo limit 1)

So it is still using the old table name. How can I ensure the new table name is used?

like image 783
user5512902 Avatar asked Dec 25 '15 10:12

user5512902


People also ask

How do you change the name of a model table?

Go to Table Tools > Design > Properties > Table Name.

How do I change the table name in migration?

To change a table name, you can do this: Schema::rename($currentTableName, $newTableName); You can use the drop or dropIfExists methods to remove an existing table: Schema::drop('users'); Schema::dropIfExists('users');

How can we use the custom table in laravel?

You can use custom table in Laravel by overriding protected $table property of Eloquent.


1 Answers

If you don't want to use the default table name (the "snake case", plural name of the class), you should specify it to the model:

protected $table = 'DomainRelatedSettings';

Check the documentation at the Table Names section.

like image 194
Pantelis Peslis Avatar answered Sep 20 '22 12:09

Pantelis Peslis