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?
Go to Table Tools > Design > Properties > Table Name.
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');
You can use custom table in Laravel by overriding protected $table property of Eloquent.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With