Laravel模型看起来旧表名

use*_*902 4 php mysql laravel laravel-5

我正在使用laravel 5并将表的名称从"domain_related_settings"更改为"DomainRelatedSettings".

我通过回滚所有迁移,更改特定迁移并再次运行它来完成此操作.在phpmyadmin中,我看到了我的新表名.

当我使用相应的模型(DomainRelatedSetting.php)作为类"DomainRelatedSetting"

$domainSettings = App\DomainRelatedSetting::where('hostname', 'localhost')->first();
Run Code Online (Sandbox Code Playgroud)

它给出以下错误:

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)
Run Code Online (Sandbox Code Playgroud)

它似乎寻找domain_related_settings.这是我的旧表名,我改为DomainRelatedSetting.

当我在类中定义新表时,它可以工作.

protected $table = 'DomainRelatedSettings';
Run Code Online (Sandbox Code Playgroud)

我在中间件中使用以下函数导致错误:$ domainSettings = App\DomainRelatedSetting :: where('hostname','localhost') - > first();

我在我的项目中搜索了一个字符串(使用phpstorm),它给出了0个结果(除了laravel日志).

是否有一个位置,除了迁移和模型,我应该更新这个?

小智 9

您需要使用以下命令在每个 Laravel 模型中指定表名

protected $table = 'name_of_table';
Run Code Online (Sandbox Code Playgroud)

所以在你的情况下

protected $table = 'DomainRelatedSettings';
Run Code Online (Sandbox Code Playgroud)


Pan*_*lis 8

如果您不想使用默认table名称("蛇案例",类的复数名称),则应将其指定为model:

protected $table = 'DomainRelatedSettings';
Run Code Online (Sandbox Code Playgroud)

请查看该Table Names部分的文档.


小智 8

您可以通过在模型上定义表属性来指定自定义表:

class theModel extends Model
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'name_of_table';
}
Run Code Online (Sandbox Code Playgroud)

如果它不起作用,请尝试从根文件夹中键入以下命令: composer dump-autoload -o