Laravel-BelongsToMany关系将忽略我的自定义(单数)表名称

问题描述 投票:0回答:1

我正在使用较旧的(laravel 5.6)代码库,该代码库的某些模型具有唯一的表名(不是我设计的。)。当我在表“ m_sector”和“ m_tasklist”之间建立新的数据透视关系时,Eloquent会自动假定表名“ m_tasklist”为复数; “ m_tasklists”。我知道这是Laravel的设计,因此我使用在Tasklist模型中定义的手动覆盖。

错误消息

"debugMessage": "SQLSTATE[42S02]: Base table or view not found: 1146 Table 'database.m_tasklists' doesn't exist (SQL: select count(*) as aggregate from `m_tasklists` where `id` in (1, 2, 3))"

任务列表模型

    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'm_tasklist';

    /**
     * A task list belongs to many sectors.
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
    public function sector(): BelongsToMany
    {
        return $this->belongsToMany(Sector::class, 'm_sector_m_tasklist', 'm_tasklist_id', 'm_sector_id');
    }

部门模型

    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'm_sector';

    /**
     * A sector belongs to many task lists.
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
    public function tasklists(): BelongsToMany
    {
        return $this->belongsToMany(Tasklist::class, 'm_sector_m_tasklist', 'm_sector_id', 'm_tasklist_id');
    }

PhpMyAdmin表名称的图片

table names

任何人都可以帮我解决这个问题,它已经伤了我一天了。

[如果有人想知道,关键约束已正确形成,迁移工作并已正确设置。如果有帮助,我可以添加它们。

php mysql eloquent laravel-5.6
1个回答
0
投票

一切看起来都正确。我唯一能想到的可能是导致此问题的原因是您正在覆盖Tasklist模型中Model的getTable方法。

© www.soinside.com 2019 - 2024. All rights reserved.