Laravel Eloquent ORM - 返回具有也属于第三个对象的对象的对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16849959/
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 ORM - Return objects having objects that also belong to a third object
提问by XuxuBelezA
First things first, I'm starting with php and MVC frameworks, I'm still not really good at this, I also couldn't do a lot of research on the problem because I don't really know how to translate this into a google search.
首先,我从 php 和 MVC 框架开始,我仍然不是很擅长这个,我也无法对这个问题做很多研究,因为我真的不知道如何将其转化为谷歌搜索。
And thats also why my question title is so funny
这也是为什么我的问题标题如此有趣
Ok, so lets say I have three models: Post, Commenter and Comment
好的,假设我有三个模型:发布、评论者和评论
The comments belong both to the posts and the commenters, so I have this code in my models, its all very simple
评论属于帖子和评论者,所以我在我的模型中有这个代码,它都很简单
class Post extends Eloquent {
public function comments() {
return $this->has_many('Comment');
}
}
...
...
class Commenter extends Eloquent {
public function comments () {
return $this->has_many('Comment');
}
}
...
...
class Comment extends Eloquent {
public function commenter () {
return $this->belongsTo('Commenter');
}
public function post () {
return $this->belongsTo('Post');
}
}
And then I want a query to list commenters ONLY IF they have comments on a given post
然后我想要一个查询来列出评论者只有他们对给定的帖子有评论
I need to go through the commenters list and then find whoever has comments belonging to that one post. (I don't really need to worry about being optimal since its an small experimental project with a small database)
我需要查看评论者列表,然后找到对那个帖子发表评论的人。(我真的不需要担心优化,因为它是一个带有小数据库的小型实验项目)
I have no Idea how to pass this on to a view using a controller, Commenter::has('comments')
will display anyone that has a comment anywhere, but I think thats the starting point. I couldnt really find an answer in the documentation also.
我不知道如何使用控制器Commenter::has('comments')
将其传递给视图,将在任何地方显示任何有评论的人,但我认为这就是起点。我也无法在文档中真正找到答案。
Please let me know if I havent made my question clear enough
如果我的问题还不够清楚,请告诉我
采纳答案by XuxuBelezA
I realized there wouldnt be a method as simple as I wanted, so I decided to go for a new Many to Many relationship...
我意识到不会有我想要的那么简单的方法,所以我决定去建立一个新的多对多关系......
I added
我加了
public function posts () {
return $this->belongsToMany('Post');
}
and
和
public function commenters () {
return $this->belongsToMany('Commenter');
}
and now I can simply use a
现在我可以简单地使用
->with ('commenters', Post::find($post_id)->commenters()->get())
in my controller to find my list of commenters
在我的控制器中找到我的评论者列表
Thanks for the help, everybody who answered.
感谢所有回答的人的帮助。
回答by Antonio Carlos Ribeiro
Having
拥有
class Comment extends Eloquent {
public function commenter () {
return $this->belongsTo('Commenter');
}
public function post () {
return $this->belongsTo('Post');
}
}
class Commenter extends Eloquent {
public function comments () {
return $this->hasMany('Comment');
}
}
class Post extends Eloquent {
public function comments () {
return $this->hasMany('Comment');
}
}
You can
你可以
$post = Post::find($givenPostID);
View::make('posts.comments.listCommenters')
->with('commenters', $post->comments()->groupBy('commenter_id')->get(['commenter_id']));
And in the View
并在视图中
@foreach($comments as $comment)
Author: <strong>{{$comment->commenter->name}}</strong>
@endforeach
Or you can create a new property
或者您可以创建一个新属性
class Post extends Eloquent {
public function getCommentersAttribute()
{
$comments = $this->comments;
foreach($this->comments as $comment)
{
/// do whatever you need to grab your list of commenters
}
return $yourListOfCommenters
}
}
And then you just refer to it anywhere you need:
然后你只需在任何需要的地方引用它:
$post->commenters