【发布时间】:2018-12-13 03:33:27
【问题描述】:
我有一个关于如何使用 Eloquent 按类别获取特定帖子的相关帖子的问题。我知道如何在纯 MySQL 中做到这一点,但我相信 Eloquent 会有更好的替代方案。
我的表格是:帖子类别 post_category (pivot)
我已经建立了必要的 Eloquent 连接,所以我想做这样的事情:$post->categories()->posts()->exclude($post)->get()。
这当然行不通。我在posts() 上收到错误,因为“Builder 没有方法posts()”,但希望你能明白。你会如何使用 Eloquent?
【问题讨论】:
-
它可能类似于
Category::whereIn($post->categories->lists('id'))->posts()->get()。 -
是的,这样的事情可以工作,但它给了我一个错误
Call to undefined method Illuminate\Database\Query\Builder::posts()