如何获取已创建帖子的所有用户的列表

时间:2017-04-17 07:47:22

标签: laravel-eloquent

我的数据库中有两个表。

    Users
         id,name
    Posts
         id,user_id,name 

    // user_id is the id who has created the post.

    How do i get list of all users that have created the posts.

    Relation in users table is like this.
    => user HasMany posts.

    The relation in Post Class 
    => post belongsTo users.


    What i have tried so far

    $uses = User::with('posts')->get();
    This returns list of all users,that has not even created the post

我是否需要为此更新或创建新关系?

1 个答案:

答案 0 :(得分:1)

你需要深入了解Laravel Eloquent。看看这个URL

顺便说一句,你可以试试这个。

$users = User::has('posts')->get();

它将返回包含帖子的所有用户的列表。

相关问题