在Laravel中嵌套多对多关系

在Laravel中嵌套多对多关系,laravel,laravel-5,orm,eloquent,Laravel,Laravel 5,Orm,Eloquent,每个用户可以遵循多个类别(多对多), 每个类别都有许多职位(一对多) 我想让所有的帖子由用户跟踪,并按日期排序 有什么方法可以用雄辩来做到这一点吗?可能有不同的方法,但我可能会用两个问题来做到这一点: // Get the IDs of the categories $categoryIds = $user->categories()->pluck('id'); // Pull the posts with those category IDs $posts = Post::whe

每个用户可以遵循多个类别(多对多), 每个类别都有许多职位(一对多)

我想让所有的帖子由用户跟踪,并按日期排序


有什么方法可以用雄辩来做到这一点吗?

可能有不同的方法,但我可能会用两个问题来做到这一点:

// 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();

你能提供一些代码吗?