Liz*_*iza 2 model relation eloquent laravel-5
如何在laravel 5中应用此查询?我有3个表,帖子,个人资料,评论
Post{id,title,body,user_id}
profile{user_id,last_name,first_name}
comments{comments,user_id,post_id}
Run Code Online (Sandbox Code Playgroud)
我想得到这种结构:
post_id title post last_name first_name
以下是评论
comments last_name fist_name
Liz*_*iza 22
//发布模型
public function comments(){
return $this->hasMany('App\Comments', 'post_id', 'id');
}
Run Code Online (Sandbox Code Playgroud)
//评论模型
public function user(){
return $this->belongsTo('App\Profile', 'user_id');
}
Run Code Online (Sandbox Code Playgroud)
//个人资料模型
public function comments(){
return $this->hasMany('App\Comments');
}
Run Code Online (Sandbox Code Playgroud)
我使用以下方法在控制器中调用它:
$posts = Posts::with('userProfile','comments', 'comments.user')->get();
Run Code Online (Sandbox Code Playgroud)
然后它工作:)谢谢你的一些帮助和想法:)
| 归档时间: |
|
| 查看次数: |
17971 次 |
| 最近记录: |