laravel 如何通过中间表在从一个模型到另一个模型中使用 Eloquent

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46283879/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 16:40:33  来源:igfitidea点击:

How To use Eloquent in From One Model to another model through intermediate table

phpmysqllaraveleloquentrelationships

提问by Emad Rashad Muhammed

i have three models

我有三个模型

Article

文章

id 
title 

Comment

评论

id
title
user_id
article_id

User

用户

id 
name

what i wanna achieve is to select one article based on its id with comments and user info that made that comment

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

like that :

像那样 :

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

this gives me article with related comments as an array of objects say comment one - comment two etc ....

这给了我带有相关评论的文章,​​作为一组对象说评论一 - 评论二等......

what i want instead of user_id in comment object i wanna it to be a user object

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

see this pic thats what i reached so far

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

screen of what i have done

我所做的事情的屏幕

using laravel 5.4

使用 Laravel 5.4

回答by yasmuru

You can use following:

您可以使用以下内容:

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

Here 'user' is the relationship you mentioned in the comments model for User.

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

回答by Vaishnav Mhetre

If you have defined the foreign key relationship in Schemas, you can define functions for Eloquent Relationship as defined in following reference link - Laravel - Eloquent Relationships.

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

You can define functions in models as follows -

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

Article -

文章 -

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

Comment -

评论 -

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

User -

用户 -

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

Now you can use like following -

现在你可以使用如下 -

   $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;

and so on...

等等...

回答by Hasnat Babur

It is far better to use ->find() at last instead of ->get() because get() returns a Collection.

最后使用 ->find() 而不是 ->get() 好得多,因为 get() 返回一个集合。

This way you will get a single object which you want instead of a Collection.

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

For example:

例如:

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