我正在使用 Laravel 5,并通过回滚所有迁移、更改特定迁移并再次运行它们,将数据库表的名称从“domain_lated_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
名称(“蛇形”,类的复数名称),您应该将其指定为 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