这些是我的架构表
Schema::create('question', function(Blueprint $table) {
$table->increments('id');
$table->text('title');
$table->string('hint')->nullable();
$table->text('explanation')->nullable();
$table->string('referencematerial')->nullable();
$table->string('imagepath')->nullable();
$table->boolean('published')->default(1);
$table->timestamps();
$table->softDeletes();
});
Schema::create('answers', function(Blueprint $table) {
$table->increments('id');
$table->string('text');
$table->boolean('iscorrect')->nullable();
$table->timestamps();
});
这是我的问答类。
class Answer extends Model
{
protected $table = 'answer';
public $timestamps = true;
protected $fillable = array('text', 'iscorrect');
public function Question()
{
return $this->belongsTo('Question');
}
}
class Question extends Model
{
protected $table = 'question';
public $timestamps = true;
protected $dates = ['deleted_at'];
protected $fillable = array('title', 'hint', 'explanation', 'referencematerial', 'imagepath', 'published');
public function Answer()
{
return $this->hasMany('Answer');
}
}
我的QuizController
public function index()
{
$questions = Question::with('answers')->get();
return $questions;
}
BadMethodCallException in Builder.php line 2251:
Call to undefined method Illuminate\Database\Query\Builder::answers()
我一直在尝试在页面上加载问题和答案。它一直给我查询生成器错误。请随意加入。
答案 0 :(得分:1)
错误基本上是告诉您,您调用的关系(answers
)不存在。
您已在模型中调用了关系Answer()
,但在查询中将其引用为answers
。
您需要将模型中的关系更新为answers()
或将查询更改为$questions = Question::with('Answer')->get();
<强>更新强>
你的关系没有正确地调用模型,它应该是:
public function Answer()
{
return $this->hasMany('App\Answer');
}