【问题标题】:How to get list of all users that have created the posts如何获取已创建帖子的所有用户的列表
【发布时间】:2017-04-17 07:47:21
【问题描述】:

我的数据库中有两个表。

    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

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

【问题讨论】:

  • 你为什么投反对票,信息不明确吗?或者这个问题没有意义。

标签: laravel-eloquent


【解决方案1】:

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

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-10
    • 1970-01-01
    • 2021-12-16
    • 2017-01-12
    • 2023-04-05
    • 2023-03-28
    相关资源
    最近更新 更多