gpt4 book ai didi

Laravel 查询生成器 - 如何按关系字段过滤

转载 作者:行者123 更新时间:2023-12-03 22:16:09 25 4
gpt4 key购买 nike

举例来说,假设我有以下内容,其中为每个戴一顶帽子的人建立了关系。

$people = People::join('hat')->get();

如何过滤我的结果,只给我帽子是红色的人?

我试过了,

$people = People::join('hat')
->where('hat.colour', 'red')
->get();

...但到目前为止还没有运气。

干杯

最佳答案

使用 whereHas() :

$people = People::whereHas('hat', function($query) {
$query->where('colour', 'red');
})->get();

关于Laravel 查询生成器 - 如何按关系字段过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51025342/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com