【问题标题】:Laravel Eloquent ORM - Return objects having objects that also belong to a third objectLaravel Eloquent ORM - 返回具有也属于第三个对象的对象的对象
【发布时间】:2013-05-26 20:13:19
【问题描述】:

首先,我是从php和MVC框架开始的,我还不是很擅长这个,我也无法对这个问题做很多研究,因为我真的不知道如何翻译这进入谷歌搜索。

这也是为什么我的问题标题如此有趣

好的,假设我有三个模型:帖子、评论者和评论

cmets 既属于帖子,也属于评论者,所以我的模型中有这段代码,非常简单

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');
    }
}

然后我想要一个查询来列出评论者,前提是他们在给定的帖子上有 cmets

我需要浏览评论者列表,然后找到拥有属于该帖子的 cmets 的人。 (我真的不需要担心是否是最优的,因为它是一个带有小型数据库的小型实验项目)

我不知道如何使用控制器将其传递给视图,Commenter::has('comments') 将在任何地方显示任何有评论的人,但我认为这就是起点。我也无法在文档中找到答案。

如果我的问题不够清楚,请告诉我

【问题讨论】:

  • 请向我们提供您的Commenter 班级。
  • 已编辑所有课程

标签: php model controller laravel eloquent


【解决方案1】:

我意识到没有我想要的那么简单的方法,所以我决定建立一个新的多对多关系......

我加了

public function posts () {
    return $this->belongsToMany('Post');
}

public function commenters () {
    return $this->belongsToMany('Commenter');
}

现在我可以简单地使用

->with ('commenters', Post::find($post_id)->commenters()->get())

在我的控制器中找到我的评论者列表

感谢所有回答的人的帮助。

【讨论】:

    【解决方案2】:

    拥有

    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');
        }
    
    }
    

    你可以

    $post = Post::find($givenPostID);
    
    View::make('posts.comments.listCommenters')
      ->with('commenters', $post->comments()->groupBy('commenter_id')->get(['commenter_id']));
    

    在视图中

    @foreach($comments as $comment)
      Author: <strong>{{$comment->commenter->name}}</strong>
    @endforeach
    

    或者您可以创建一个新属性

    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
        }
    
    }
    

    然后您只需在需要的任何地方引用它:

    $post->commenters
    

    【讨论】:

    • 但如果我这样做,如果有多个评论属于他,它将列出同一作者两次。我需要先浏览评论者列表,然后找到拥有属于该特定帖子的 cmets 的人
    • 你是对的,但为了解决这个问题,我刚刚在上面添加了一个 groupBy。我只是给你一些想法,并没有真正解决你的问题,如果你认为你需要通过 cmets 列表然后获得所有评论者,你可能需要编写更多代码,一个查询来为你做这件事或a 在您的模型上创建一个新方法以获取该评论者列表。
    • groupBy 方法实际上可能会有所帮助,感谢您的想法,但我仍然不知道它是否对我有用。为了创建这个新方法,我需要做什么?
    • 刚刚编辑添加属性。要创建一个方法,您只需从方法名称中删除 get*Attribute。
    • 谢谢!我会尝试使用它
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-12-14
    • 2013-05-20
    • 1970-01-01
    • 1970-01-01
    • 2021-06-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多