Welcome To ITSolutionsGuides,
The Laravel team released v10.47 which added the added feature of whereAny(), which was a new methods can search against multiple columns or logic. While we need to search multiple columns we will create the multiple or for building the search query. Query Builder whereAny() has reduced the query much simpler. In Laravel, the whereAny() method in Query Builder allows filtering records based on any of the given conditions, providing flexibility and efficiency in querying databases. Lets see how to use Query Builder whereAny()
// Before using `orWhere`
User::query()
->where(function ($query) use ($search) {
$query
->where('first_name', 'LIKE', $search)
->orWhere('last_name', 'LIKE', $search)
->orWhere('email', 'LIKE', $search)
->orWhere('phone', 'LIKE', $search);
});
// Using `whereAny`
User::whereAny(
[
'first_name',
'last_name',
'email',
'phone'
],
'LIKE',
"%$search%"
);
We hope it helps everyone. Thanks for supporting ITSolutionsGuides and keep supporting us also follow us in social media platforms.
Be the first to know about releases and tutorial news and solutions.
We care about your data in our privacy policy.
ITSolutionsGuides was started mainly to provide good and quality web solutions for all the developers. We provide tutorials to support all the developers and also we try to provide solutions to the errors we face while coding.
Copyright © 2023 - 2024 All rights reserved | ITSolutionsGuides