Laravel 模型看起来像旧表名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34462190/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel model looks to old table name
提问by user5512902
I am using laravel 5 and have change the name of a table from "domain_related_settings" to "DomainRelatedSettings".
我正在使用 laravel 5 并将表的名称从“domain_related_settings”更改为“DomainRelatedSettings”。
I did this by rolling back all migrations, change the specific migration and run them again. In phpmyadmin i see my new tablename.
我通过回滚所有迁移、更改特定迁移并再次运行它们来做到这一点。在 phpmyadmin 中,我看到了我的新表名。
When i use the corresponding model (DomainRelatedSetting.php) with as a class "DomainRelatedSetting"
当我使用相应的模型(DomainRelatedSetting.php)作为类“DomainRelatedSetting”
$domainSettings = App\DomainRelatedSetting::where('hostname', 'localhost')->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` = localhost limit 1)
It seems to look to domain_related_settings. That was my old tablename, which i changed to DomainRelatedSetting.
似乎在看 domain_related_settings。那是我的旧表名,我将其更改为 DomainRelatedSetting。
When i define the new table in the class it works.
当我在类中定义新表时,它会起作用。
protected $table = 'DomainRelatedSettings';
I use the following function in middleware which is causing the error: $domainSettings = App\DomainRelatedSetting::where('hostname', 'localhost')->first();
我在导致错误的中间件中使用了以下函数: $domainSettings = App\DomainRelatedSetting::where('hostname', 'localhost')->first();
I did a a search for the string in my project (using phpstorm) and it gives 0 results (except from the laravel log).
我在我的项目中搜索了字符串(使用 phpstorm),它给出了 0 个结果(除了 laravel 日志)。
Is there a location except from the migration and the model where i should update this?
除了迁移和我应该更新的模型之外,还有其他位置吗?
回答by Pantelis Peslis
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
:
如果您不想使用默认table
名称(“蛇形大小写”,类的复数名称),则应将其指定为model
:
protected $table = 'DomainRelatedSettings';
Check the documentation at the Table Names
section.
检查 部分中的文档Table Names
。
回答by Vojko
You need to specify the table name inside the each Laravel model by using
您需要使用以下命令在每个 Laravel 模型中指定表名
protected $table = 'name_of_table';
so in your case
所以在你的情况下
protected $table = 'DomainRelatedSettings';
回答by Nino Giovanny
If you specify the real name of the tables in your models but still the same problem, try:
如果您在模型中指定了表的真实名称,但仍然存在同样的问题,请尝试:
composer dumpautoload