gpt4 book ai didi

Laravel 多个 whereHas 关系标准

转载 作者:行者123 更新时间:2023-12-02 10:42:03 26 4
gpt4 key购买 nike

我有两个表 - 联系人和访问:

联系人表

id    | name          
----- | -------
1 | Joe
2 | Sally

访问表

id    | contact_id | pathname  | referrer                
----- | ------- | ------- | -------
1 | 1 | about | google
2 | 1 | pricing | null
3 | 1 | signup | null
4 | 2 | about | null
5 | 2 | signup | null

使用 eloquent,我想检索所有具有 路径名 = 'signup' 和 referrer = 'google 的联系人 '。

到目前为止我得到的是:

Contact::whereHas('visits', function($query) {
$query->where('pathname','=','signup');
})
->orWhereHas('visits', function($query) {
$query->where('referrer','=','google');
})
->get();

它可以正确检索访问过定价或注册页面的所有联系人。

但是,此示例还会检索 Sally(来自上面的示例表),因为她访问了注册,但未被 Google 引用。我需要一种方法来仅检索 Joe,他是由 Google 推荐并访问过定价的。

有什么想法吗?提前致谢!

最佳答案

您可以使用:

Contact::whereHas('visits', function($query) {
$query->where('pathname','=','signup');
})
->whereHas('visits', function($query) {
$query->where('referrer','=','google');
})
->get();

关于Laravel 多个 whereHas 关系标准,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26194771/

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