Laravel Eloquent 有很多关系
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30412755/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel Eloquent hasMany relationship
提问by usrNotFound
Hi I am new to Laravel and currently in am using Laravel 4.2. I am trying to create an application where I have users, posts and comments table and have the following model
嗨,我是 Laravel 的新手,目前正在使用 Laravel 4.2。我正在尝试创建一个应用程序,其中包含用户、帖子和评论表并具有以下模型
User Model
用户模型
function posts() {
return $this->hasMany('Post');
}
function comments(){
return $this->hasMany('Comment');
}
Post Model
岗位模型
function users() {
return $this->belongsTo('User');
}
function comments() {
return $this->hasMany('Comment');
}
Comment Model
评论模型
function posts(){
return $this->belongsTo('Post');
}
function users(){
return $this->belongsTo('User');
}
What I want to achieve :
我想要实现的目标:
user's Post and comment for Post
用户的帖子和帖子的评论
eg:: User1 -> Post one -> comment for post one
What I have done So far:::
到目前为止我做了什么:::
$user = User::find(1);
$post = $user->posts()->get();
I am able to get post but how do i get comment for specific post??
我可以得到帖子,但如何获得特定帖子的评论?
Update
更新
Thanks to @Bogdan for his help I am able to get post and comment for user. But as usual had another problem.
感谢@Bogdan 的帮助,我能够获得用户的帖子和评论。但像往常一样有另一个问题。
What I got:
我得到了什么:
foreach($user->post AS $post) {
foreach ($post->comment AS $comment ) {
$comment->commentcontent; $user->uername;
}
}//first foreach
This is what I get
这就是我得到的
comment one by user1.
comment two by user1.
But in reality comment one is created by user1 and comment two is created by user2.
但实际上评论一是由用户 1 创建的,而评论二是由用户 2 创建的。
Thank you for your help in advance. Can post full code if needed.
提前谢谢你的帮助。如果需要,可以发布完整的代码。
回答by Bogdan
When you retrieve a users' posts with $user->posts()->get()
you get a Collection
of Models
, on which you can use find
to get the specific post you want. Then you can retrieve the post's comment the same way you retrieved the users' posts:
当您$user->posts()->get()
使用Collection
of检索用户的帖子时Models
,您可以使用它find
来获取您想要的特定帖子。然后,您可以像检索用户的帖子一样检索帖子的评论:
$user = User::find(1);
$post = $user->posts()->find($postId);
$comments = $post->comments;
If you want to iterate over the entire posts collection, you can do this and access comments for each post individually, without filtering for a specific post:
如果您想遍历整个帖子集合,您可以执行此操作并单独访问每个帖子的评论,而无需过滤特定帖子:
foreach ($user->posts as $post)
{
$comments = $post->comments;
}
Also, for future reference, accesing the relation as a property will return a Collection, while accessing the relation as a method will return a Query Builder instance. So $user->posts
is the same as $user->posts()->get()
.
此外,为了将来参考,将关系作为属性访问将返回一个集合,而作为方法访问关系将返回一个查询构建器实例。所以$user->posts
是一样的$user->posts()->get()
。
回答by wiseodd
You want to get user that made the comment, so get that user from the comment object.
您想获取发表评论的用户,因此从评论对象中获取该用户。
Try this
尝试这个
$user = User::find(1);
$posts = $user->posts;
foreach ($posts as $post) {
foreach ($post->comments as $comment) {
echo $comment->commentcontent;
echo $comment->users;
}
}