【问题标题】:laravel one to many relationship column not foundlaravel 一对多关系列未找到
【发布时间】:2015-05-25 10:29:03
【问题描述】:

我有以下型号:

class Question extends Eloquent 
{
   public  function quiz()
   {
      return $this->belongsTo('Quiz','id_quiz','id');
   }
   public  function answer()
   {
      return $this->hasMany('Answer');
   }
}

class Answer extends Eloquent
{
   public function question()
   {
      return $this->belongsTo('Question','id_question','id');
   }
}

在我使用的控制器中:

$questions =  Question::with('answer')->whereHas(
     'quiz',  function($q) use($id) {$q->where('id', $id);
})->get();

我收到以下错误:

找不到列:1054 未知列 'answers.question_id' in 'where 子句' (SQL: select * from answers where answers.question_id in (10, 11, 12, 13)))

【问题讨论】:

  • 错误已经告诉你你的问题,阅读它。
  • 是的,但我没有任何列名 question_id,我有 id_question

标签: php mysql laravel laravel-4 eloquent


【解决方案1】:

您应该以这种方式在您的 Question 模型中定义您的 answer 关系:

public  function answer(){
    return $this->hasMany('Answer','id_question','id');
}

因为正如您所说,您有列 id_question 而不是 question_id 在这种情况下默认值是什么。

【讨论】:

    猜你喜欢
    • 2017-11-07
    • 2021-09-23
    • 2020-03-05
    • 1970-01-01
    • 2019-06-03
    • 2018-06-15
    • 2020-04-14
    • 2018-09-27
    相关资源
    最近更新 更多