【发布时间】:2018-09-11 22:33:24
【问题描述】:
Laravel Eloquent 查询构建器问题
您好,我在尝试获取 slug = $slug 所在的所有行时遇到问题。
我会详细解释:
我有两张桌子,cards 和 card_categories。
cards has category_id.
card_categories has slug.
我需要的是一个查询,它返回所有包含蛞蝓的卡片。
例如我做了这个:
$cards = Card::leftJoin('card_categories', 'cards.id', '=', 'card_categories.id')
->select('cards.*', 'card_categories.*')
->where('card_categories.slug', $slug)
->paginate(5);
但是发生的情况是每个类别只返回 1 行。
我不知道怎么了。
非常感谢。
【问题讨论】:
-
您能否在查询中调用
->toSql()以确定实际的sql 查询是什么样的?我很少直接使用查询生成器(主要使用雄辩的模型)..我想帮忙,但我只怀疑leftJoin上的事情会变得很奇怪,或者您的数据只是每个类别都有一个..