我正在使用Laravel 5,并已将数据库表的名称从"更改为";domain_related_settings"至";DomainRelatedSettings;通过回滚所有迁移,更改特定的迁移,然后再次运行它们。新表名将反映在数据库中。
但当我在这样的语句中使用相应的模型DomainRelatedSetting
时:
$domainSettings = DomainRelatedSetting::where('hostname', 'foo')->first();
它给出以下错误:
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)
所以它仍然使用旧的表名。如何确保使用新表名?
如果您不想使用默认的table
名称("snake case",类的复数名称),您应该将其指定为model
:
protected $table = 'DomainRelatedSettings';
查看Table Names
部分的文档。
您可以通过在模型上定义表属性来指定自定义表:
class theModel extends Model
{
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'name_of_table';
}
如果它不起作用,请尝试从根文件夹中键入以下命令:composer dump-autoload -o
您需要使用在每个Laravel模型中指定表名
protected $table = 'name_of_table';
所以在你的情况下
protected $table = 'DomainRelatedSettings';
如果您在模型中指定了表的真实名称,但仍然存在相同的问题,请尝试:
composer dump-autoload