【问题标题】:How To use Eloquent in From One Model to another model through intermediate table如何通过中间表在从一个模型到另一个模型中使用 Eloquent
【发布时间】:2018-02-27 06:16:31
【问题描述】:

我有三个模型

文章

id 
title 

评论

id
title
user_id
article_id

用户

id 
name

我想要实现的是根据它的 id 选择一篇文章,其中包含发表该评论的 cmets 和用户信息

像这样:

$article = Article::find($id -- say 1)->with('comments' -- this is a relation in Article Model)->get(); 

这给了我与相关 cmets 相关的文章,作为对象数组说评论一 - 评论二等....

我想要什么而不是评论对象中的 user_id 我希望它成为一个用户对象

看到这张照片,这就是我到目前为止所达到的目标

使用 laravel 5.4

【问题讨论】:

    标签: php mysql laravel eloquent relationships


    【解决方案1】:

    您可以使用以下内容:

    $articles = Article::find($id)->with('comments', 'comments.user')->get();
    

    这里的“用户”是您在 cmets 模型中提到的用户关系。

    【讨论】:

    • 您甚至可以将其缩短为:$articles = Article::find($id)->with('comments.user')->get();
    • 仅供参考,您还可以使用 eloquent hasManyThrough 关系绕过 cmets 表。
    • 好的,谢谢,如果我有另一个表,该表具有 follower_id 和以下 id 以及用户模型中称为 follower() 和另一个称为 follower 的关系,我需要计算关注者和关注者的数量添加此手册的用户
    【解决方案2】:

    如果您已经在 Schemas 中定义了外键关系,您可以为 Eloquent 关系定义函数,如以下参考链接中定义的 - Laravel - Eloquent Relationships.

    您可以在模型中定义函数如下 -

    文章 -

      class Article extends Model
      {
         ...
    
         public function comments(){
    
             // Accessing comments posted to that article.
    
             return $this->hasMany(\App\Comment::class);
         }
    
         // Create a foreign key to refer the user who created the article. I've referred it here as 'created_by'. That would keep relationship circle complete. You may ignore it if you want.
    
         public define user(){
    
             // Accessing user who posted the article
    
             return $this->hasOne(\App\User::class, 'id', 'created_by');
         }
      }
    

    评论 -

      class Comment extends Model
      {
         ...
    
         public function article(){
    
             // Accessing article to which the particular comment was posted
    
             return $this->hasOne(\App\Article::class, 'id', 'article_id');
         }
    
         public function user(){
    
             // Accessing user who posted the comment
    
             return $this->hasOne(\App\User::class, 'id', 'user_id');
         }
      }
    

    用户 -

      class User extends Models
      {
         ...
    
         public function articles(){
    
             // Accessing articles posted by a user
    
             return $this->hasMany(\App\Article::class);
         }
    
         public function comments(){
    
             // Accessing comments posted by a user
    
             return $this->hasMany(\App\Comment::class);
         }
      }
    

    现在你可以像下面这样使用 -

       $article = Article::findOrFail($id);
       $comments = $article->comments;
       $article_user = $article->user;
       $comment_user = Comment::findOrFail($commnet_id)->user;
       $users_comments = User::findOrFail($user_id)->comments;
       $users_articles = User::findOrFail($user_id)->articles;
    

    等等……

    【讨论】:

      【解决方案3】:

      最后使用 ->find() 比使用 ->get() 好得多,因为 get() 返回一个 Collection。

      这样你会得到一个你想要的对象而不是一个集合。

      例如:

      $commentableObj = Post::with(['comments'])
                        ->withCount(['comments'])
                        ->findOrFail($commentable->id);
      

      【讨论】:

        猜你喜欢
        • 2020-06-15
        • 2023-03-19
        • 2022-01-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-05-13
        • 2014-12-07
        • 1970-01-01
        相关资源
        最近更新 更多