【问题标题】:Nested many-to-many relationships in Laravel [closed]Laravel中嵌套的多对多关系[关闭]
【发布时间】:2019-01-24 11:42:17
【问题描述】:

每个用户可以关注多个类别(多对多), 每个类别都有很多帖子(一对多),

我想获取用户关注的所有帖子并按日期对其进行排序。

有没有办法用 Eloquent 做到这一点?

【问题讨论】:

  • 你能提供一些代码吗?

标签: laravel laravel-5 orm eloquent


【解决方案1】:

可能有不同的方式,但我可能会通过两个查询来做到这一点:

// Get the IDs of the categories
$categoryIds = $user->categories()->pluck('id');

// Pull the posts with those category IDs
$posts = Post::whereIn('category_id', $categoryIds)->get();

【讨论】:

    猜你喜欢
    • 2014-10-14
    • 2023-03-31
    • 2021-09-07
    • 2020-11-08
    • 2011-10-21
    • 2018-06-15
    • 2017-05-22
    • 1970-01-01
    相关资源
    最近更新 更多