英文:
Laravel Eloquent How to apply condition on "with" datas on the output?
问题
以下是您的代码的翻译部分:
$prestations = Prestation::with(
[
'service' => function($service) use($searchService) {
$service->select(['id','name'])->where('name', 'regexp', "/$searchService/i");
},
'facility' => function($facility) use($searchPartenaire) {
$facility->select(['id','name'])->where('name', 'regexp', "/$searchPartenaire/i");
}
]
)
->where('name', 'regexp', "/$search/i")
->orderBy($orderBy, $orderDirection)
->simplePaginate(50);
$res = [
'results' => $prestations,
'total' => Prestation::all()->count(),
];
问题在于输出中,当“service”和“facility”名称与$searchService和$searchPartenaire不相等时,这些值会被替换为"null"。因此,我不希望在输出中有与搜索变量不相等的值。感谢您。
英文:
here my code :
$prestations = Prestation::with(
[
'service' => function($service) use($searchService) {
$service->select(['id','name'])->where('name', 'regexp', "/$searchService/i");
},
'facility' => function($facility) use($searchPartenaire) {
$facility->select(['id','name'])->where('name', 'regexp', "/$searchPartenaire/i");
}
]
)
->where('name', 'regexp', "/$search/i")
->orderBy($orderBy, $orderDirection)
->simplePaginate(50);
$res = [
'results' => $prestations,
'total' => Prestation::all()->count(),
];
The problem is that in the output of all datas where "service" and "facility" names are not equal on the $searchService and $searchPartenaire the values are replaced by "null".
So i don't want to have values in the output where the search variables are not equals.
Thank you.
答案1
得分: 1
1st create instances of Prestation $prestations = Prestation::with('service','facility')
then apply the condition; this is a good approach in search.
英文:
you can try like this
$prestations = Prestation::with('service','facility');
$prestations->whereHas('service', function ($query) use ($searchPartenaire) {
$query->Where('name', 'like', '%' . $searchPartenaire . '%');
});
$prestations->whereHas('facility', function ($query) use ($searchPartenaire) {
$query->Where('name', 'like', '%' . $searchPartenaire . '%');
});
$prestations->where('name', 'like', '%'.$search.'%')
->orderBy($orderBy, $orderDirection)
->simplePaginate(50);
return $res = [
'results' => $prestations,
'total' => Prestation::all()->count(),
];
1st create instances of Prestation $prestations = Prestation::with('service','facility')
then apply the condtion this is good approach in seach
答案2
得分: 0
这是Kamlesh Paul建议后的我的代码:
$prestations = Prestation::with('service','facility');
$prestations->whereHas('service', function ($query) use ($searchService) {
$query->where('name', 'regexp', "/$searchService/i");
});
$prestations->whereHas('facility', function ($query) use ($searchPartenaire) {
$query->where('name', 'regexp', "/$searchPartenaire/i");
});
$prestations->where('name', 'regexp', "/$search/i")
->orderBy($orderBy, $orderDirection)
->simplePaginate(50);
$res = [
'results' => $prestations,
'total' => Prestation::all()->count(),
];
return $res;
但是存在无限的HTTP请求调用问题,我认为问题可能出在当where
条件找不到相等的名称时。有人有建议吗?谢谢。
英文:
Here my code after Kamlesh Paul suggestion :
$prestations = Prestation::with('service','facility');
$prestations->whereHas('service', function ($query) use ($searchService) {
$query->where('name', 'regexp', "/$searchService/i");
});
$prestations->whereHas('facility', function ($query) use ($searchPartenaire) {
$query->where('name', 'regexp', "/$searchPartenaire/i");
});
$prestations->where('name', 'regexp', "/$search/i")
->orderBy($orderBy, $orderDirection)
->simplePaginate(50);
$res = [
'results' => $prestations,
'total' => Prestation::all()->count(),
];
return $res;
But there is an infinite calls of http request, i think that the problem is when the where don't find an equal name, anyone have a suggest ?
Thank's.
答案3
得分: 0
我终于找到了一个非常相似的解决方案:
$prestations = Prestation::with('service', 'facility')
->whereHas('service', function ($query) use ($searchService) {
$query->where('name', 'regexp', "/$searchService/i");
})
->whereHas('facility', function ($query) use ($searchPartenaire) {
$query->where('name', 'regexp', "/$searchPartenaire/i");
})
->where('name', 'regexp', "/$search/i")
->orderBy($orderBy, $orderDirection)
->simplePaginate(50);
$res = [
'results' => $prestations,
'total' => Prestation::all()->count(),
];
感谢您的帮助。
英文:
I finnaly found a solution very similar :
$prestations = Prestation::with('service','facility')
->whereHas('service', function ($query) use ($searchService) {
$query->where('name', 'regexp', "/$searchService/i");
})
->whereHas('facility', function ($query) use ($searchPartenaire) {
$query->where('name', 'regexp', "/$searchPartenaire/i");
})
->where('name', 'regexp', "/$search/i")
->orderBy($orderBy, $orderDirection)
->simplePaginate(50);
$res = [
'results' => $prestations,
'total' => Prestation::all()->count(),
];
Thank's for your help.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论