我开始使用Laravel,但我无法显示来自对象的信息:
$questions = DB::table('Questions')
->join('Users', 'Users.id', '=', 'Questions.user_id')
->join('Profiles', 'Profiles.user_id', '=', 'Users.id')
->leftjoin('Answers', 'Answers.question_id', '=', 'Questions.id')
->orderBy('Questions.created_at','desc')
->get();
查询工作正常,但是当我尝试在刀片模板中显示这样的内容时:
@foreach ($question->answers as $answer )
...
endforeach
我收到了消息
未定义属性:stdClass :: $ answers(查看:C:... \ app \ views \ home.blade.php)
这是问答配置
class Question extends Eloquent {
public function user()
{
return $this->belongsTo('User');
}
public function answers()
{
return $this->hasMany('Answer');
}
}
class Answer extends Eloquent {
public function question()
{
return $this->belongsTo('Question');
}
}
users:
-id
-email
-photo
-name
questions:
-id
-content
-user_id
answers:
-id
-content
-user_id
-question_id