【问题标题】:Laravel many-to-many relation with custom table names and IDsLaravel 与自定义表名和 ID 的多对多关系
【发布时间】:2019-01-29 15:07:14
【问题描述】:

你好,所以我在 Question [table name: tblquestion, id: que_id] 和 Agecategory [table name: tblagecategory 之间有多对多的关系>,id:aca_id]。他们共享名为 QuestionAgecategory 的表 [表名:tblquestionagecategory,id:qac_id]。

我想注意的是,所有的 IDS 和表名都是自定义命名的,而不是根据典型的 Laravel 语法。

我正在尝试在 Laravel 中关联它们。到目前为止,当我尝试查看 $question->agecategories;

时它返回 null

$question->agecategories; => 空

但是它里面有记录,并且在 $question = App\Question::find(1); 之后返回这个

$question = App\Question::find(1); => 应用\问题{#2901 que_id: 1, que_name: "你好",

问题模型

class Question extends Model
{
    protected $table = 'tblquestion';
    protected $primaryKey = 'que_id';
    protected $keyType = 'integer'; 
    public $incrementing = true;
    public $timestamps = false;

    public function agecategories() 
    {
        return $this->belongsToMany('App\Agecategory');
    }
}

年龄分类模型

class Agecategory extends Model
{
    protected $table = 'tblagecategory';
    protected $primaryKey = 'aca_id';
    protected $keyType = 'integer'; 
    public $incrementing = true;

    public function questions() 
    {
        return $this->belongsToMany('App\Question');
    }
}

QuestionAgecategory 模型

class QuestionAgecategory extends Model
{
    protected $table = 'tblquestionagecategory';
    protected $primaryKey = 'qac_id';
    protected $keyType = 'integer'; 
    public $incrementing = true;
}

迁移

      Schema::create('tblquestion', function (Blueprint $table) {
          $table->increments('que_id');
          $table->string('que_name', 128);
      });


      Schema::create('tblagecategory', function (Blueprint $table) {
          $table->increments('aca_id');
          $table->timestamps();
      });

      Schema::create('tblquestionagecategory', function (Blueprint $table) {
          $table->increments('qac_id');
          $table->integer('qac_que_id')->unsigned();
          $table->integer('qac_aca_id')->unsigned();
          $table->foreign('qac_que_id')->references('que_id')->on('tblquestion');
          $table->foreign('qac_aca_id')->references('aca_id')->on('tblagecategory');
      });

【问题讨论】:

    标签: laravel migration many-to-many entity-relationship


    【解决方案1】:

    您正在使用自定义列和自定义数据库命名。

    Your Belongs to many 期待一个不存在的数据透视表tblquestion_tblagecategory。正如前面的答案所述,您应该更改您的 belongsToMany 以搜索自定义表和列。

    https://laravel.com/docs/5.6/eloquent-relationships#many-to-many

    在你的问题模型中改成这个

    public function agecategories() 
    {
        return $this->belongsToMany('App\Agecategory', 'tblquestionagecategory', 'qac_que_id', 'qac_aca_id');
    }
    

    而且,在您的其他年龄类别模型中

    public function questions() 
    {
        return $this->belongsToMany('App\Question', 'tblquestionagecategory', 'qac_aca_id', 'qac_que_id');
    }
    

    【讨论】:

    • 我仍然收到此错误:$agecategory->questions;使用消息'SQLSTATE [42S02] 照亮/数据库/查询异常:找不到基表或视图:1146 表'yamldb.agecategory_question'不存在(SQL:选择tblquestion.*、agecategory_question.agecategory_aca_id as @ 987654328@, agecategory_question.question_que_id as pivot_question_que_id from tblquestion 内部连接 ​​agecategory_question on tblquestion.que_id = agecategory_question@.agecategory_aca_idquestion_que_id@9865438@1 '
    • 那是您进行此更改的时间?这很奇怪,因为您正在定义表名“tblquestionagecategory”,这就是它应该寻找的......
    • 是的,我知道。而且我真的无能为力,因为我从 yaml 文件中获取了这些表名和所有详细信息,所以这就是 Laravel 语法不常用表名的原因。我还弄乱了 ID(首先我尝试了您的解决方案,然后我尝试从 Questions 表中添加 ID 并将其与 tblquestionagecategory 的 ID public function questions() { return $this->belongsToMany('App\Question', 'tblquestionagecategory', 'aca_id', 'qac_aca_id'); } 中的 ID 连接起来,但这也没有用。
    • 我在做一个长镜头......但也许作曲家 dumpauto 或 php artisan dump-autoload
    • 是的,我知道我将其切换回这些键,但即使在我再次尝试 composer dump-autoload 之后它仍然采用旧值。但是 Agecategory 中的功能有效!所以它只是来自仍然顽固的问题模型功能。但我会将此回复标记为我的问题答案:)
    【解决方案2】:

    在问题模型中添加以下关系函数

    public function agecategories() 
    {
        return $this->belongsToMany('App\Agecategory', 'tblquestionagecategory', 'qac_que_id', 'qac_aca_id');
    }
    

    【讨论】:

    • 即使我添加了 tblquestionagecategory 和键,它仍然返回 null。我在想我需要添加表格。但是使用自定义名称使关系起作用确实很棘手。我是否需要在 QuestionAgecategory 模型中定义一些东西?
    • QuestionAgecategory 模型不需要。
    【解决方案3】:

    我遇到了同样的问题,但我只需要声明没有前缀的外键:

    public function agecategories() 
    {
        return $this->belongsToMany('App\Agecategory', 'tblquestionagecategory', 'que_id', 'aca_id');
    }
    

    【讨论】:

      猜你喜欢
      • 2019-02-09
      • 1970-01-01
      • 1970-01-01
      • 2021-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多