I have the following code snippet for retrieving requests made by a user , here I need to apply 2 filters
A mandatory filter for restricting items requested by logged in user (line # 2).
An optional set of filters based on keywords entered by the user (inside the
if ($keywords)
branch).
It's working fine if $keywords
is empty (user hasn't entered anything), however, when $keywords
has something, the first mandatory user query is totally ignored and only what's inside the branch gets executed, . the strange things is that inside the branch I'm reusing the filtered set already ($requestItems = $requestItems->whereHas
)
In a nutshell, whereHas
is causing where
to be ignored although the queries are connected together!
$keywords = request()->get('keywords');
$requestItems = RequestItem::where('requestor_id', '=', Auth::id());
if ($keywords) {
$requestItems = $requestItems->whereHas('relParentRequest', function($query) use ($keywords) {
$query->where('display_name', '=', $keywords);
})->orWhereHas('relService', function($query) use ($keywords) {
$query->where('name', 'like', "%$keywords%");
})->orWhereHas('relEntityState', function($query) use ($keywords) {
$query->where('name', 'like', "%$keywords%");
})->orWhereHas('relRequestor', function($query) use ($keywords) {
$query->where('name', 'like', "%$keywords%");
})->orWhere('display_name', '=', $keywords
)->orWhere('created_at', 'like', "%$keywords%");
}
$requestItems = $requestItems->paginate(10);
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire