gpt4 book ai didi

php - 拉拉维尔 : Nested relation search

转载 作者:行者123 更新时间:2023-11-29 10:19:32 26 4
gpt4 key购买 nike

我有两个嵌套关系,一个是 requester.linkrequester.company,我希望两者都搜索单词 $request->search .

代码是不言自明的:

$clients = \App\AcceptedConnection::with('requester.link', 'requester.company')
->where('accepter_id', Auth::user()->id)
->orWhereHas('requester.link', function ($query) use ($request) {
$query->orWhere('name', $request->search);
})->orWhereHas('requester.company', function ($query) use ($request) {
$query->orWhere('email', $request->search);
$query->orWhere('firstname', $request->search);
$query->orWhere('lastname', $request->search);
})->get();

它为我提供任何搜索的记录,即使搜索不匹配,它也会返回记录,搜索任何内容,它都会为您提供记录,

我错过了什么?

最佳答案

您正在检查 accepter_id=Auth::user()->id OR 记录是否与您的搜索词匹配。

您必须将 whereHas() 检查包装到闭包中:

$clients = \App\AcceptedConnection::with('requester.link', 'requester.company')
->where('accepter_id', Auth::user()->id)
->where(function($query) use ($request) {
$query->whereHas('requester.link', function ($query) use ($request) {
$query->orWhere('name', $request->search);
})->orWhereHas('requester.company', function ($query) use ($request) {
$query->orWhere('email', $request->search);
$query->orWhere('firstname', $request->search);
$query->orWhere('lastname', $request->search);
});
})->get()

关于php - 拉拉维尔 : Nested relation search,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49574201/

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