Updated April 13, 2023
Introduction to Laravel Where Like
If you ever want to build a powerful, dynamic web-application, a strong language and a database are not the only things that you need to make it perform swiftly. Alongside a powerful language, a good framework is what you need to make it competent and perform swiftly along with its contemporaries. And when it comes to PHP, Laravel is undoubtedly one of the solutions. Nearly about 75% of the websites today have chosen Php as their backend development platform, including giants like Facebook and Wikipedia. How many may have the question of what a framework does? Well, be calm. I will explain it to you.
Today, in this context, as we will discuss the WHERE LIKE query in Laravel, I will try to make your vision clear about the need for a framework.
Framework – A Brief
A framework is indeed required for a web application to make it enable to perform all the tasks meant for it. Whether it comes to a convenient migration system or you need an integrated for module testing, a reliable framework is required to get your job done. And in that case, Laravel has become predominant over its other contemporaries. Since its inception, it has become increasingly popular among the PHP developers. Whenever a dynamic, sophisticated application is required to be built using PHP, most developers choose to make way for Laravel. The well-documented scripture of Laravel makes it easier to understand and easy to learn for everyone, which also adds to the reason why it became popular.
When it is about the functionality of your application, the role of database query holds a crucial part. After all, it is the functionality of the system which bears the fruit of your hard work. And it is in this part where several developers face challenges. Moreover, if it is an ORM, just like Eloquent, finding an efficient way indeed troubles a lot.
The WHERE() Method
Certainly, WHERE() is the most commonly used clause, be it any database. Also, many other conditions can be used inside this clause as subqueries, which makes its use a lot more convenient. Some of the conditions commonly used inside the WHERE clause are AND, OR, IN NULL, NOT NULL, GREATER THAN, EQUALS TO, NOT EQUALS TO & soon.
So, in this post, we will present to you how you can use WHERE LIKE query in your Laravel Eloquent ORM. For this, we will show you an example of how you can provide a search model for the user utilizing the WHERE LIKE query in Laravel.
1. Eloquent Searching Model In Laravel
User::query()
->where('name', 'LIKE', "%{$searchTerm}%")
->orWhere('email', 'LIKE', "%{$searchTerm}%")
->get();
The above query will provide all the records as a result that have a name or email containing the string $searchTerm.
2. By using macro
Now, if we want to add a search to all the models along with the user, then we have to add a macro to the Laravel Eloquent’s Query Builder.
Here is our example:
use Illuminate\Database\Eloquent\Builder;
// ...
Builder::macro('whereLike', function(string $attribute, string $searchTerm) {
return $this->orWhere($attribute, 'LIKE', "%{$searchTerm}%");
});
And now, we may search our model in the following way:
User::query()
->whereLike('name', $searchTerm)
->whereLike('name', $searchTerm)
->get();
3. Enhancing the functionality of our macro
Well, we can see our basic requirement has been accomplished. However, a good or expert developer will not like that he has to use the where like call each time for searching each of the attributes. We can easily fix that issue if we apply some enhancements to our macro. So, let’s try this out.
Builder::macro('whereLike', function($attribute, string $TermToSearch) {
foreach(array_wrap($attribute) as $attribute) {
$this->orWhere($attribute, 'LIKE', "%{$TermToSearch}%");
}
return $this;
});
Therefore, the above one is an example of our enhancement to the previous macro, where we seek the help of a nice Laravel helper array_wrap.
array_wrap is a Laravel helper in which when we input an array, it returns that array, and when given anything other than an array, it wraps it up in that array.
As now our problem been fixed, you may the above macro in the following way:
// onecolumnsearch
User::whereLike('UserName', $TermToSearch)->get();
// multiple column search
User::whereLike([UserName, 'EmailAddress'], $TermToSearch)->get();
4. Further Fixation
As we can see, our search model looks quite good after the improvement in our macro and pretty handy as well. But only if you notice keenly there is still a bug in it. Take a look in this query:
User::query()
->where('role', 'admin')
->whereLike(['name', 'email'], 'john')
->get();
As we can see, in this above query, we have used or Where in our whereLike macro. The reason behind doing so is that it will not only return only the users with the role of admin but will result in returning users with the role of admin and also others which contains John in their email or name.
It is the bug I’m talking and hence let’s fix this by wrapping the or Wherein a function. However, this is quite similar to that of setting up parenthesis in a query. Fix:
Builder::macro('whereLike', function ($attribute, string $TermToSearch) {
$this->where(function (Builder $query) use ($attribute, $TermToSearch) {
foreach (array_wrap($attribute) as $attribute) {
$query->orWhere($attribute, 'LIKE', "%{$TermToSearch}%");
}
});
return $this;
});
Since it has been, the query will only give results of users with the role of admin and contains John in their email or name.
Output:
Conclusion – Laravel Where Like
However, in Laravel you will see like the query is used inside the where clause. Now, if you take a deeper note at this point, you will be able to discover the reason behind it. The main essence of using the like query inside the where clause is to filter the match value of the select column of the table. If we follow our above example, it too depicts the same thing.
we hope the above example proved helpful to you in better understanding of how to use where like function in Laravel. However, if you wish to work on building a lightweight yet sensible search model then you may seek the help of the above example too.
Recommended Articles
We hope that this EDUCBA information on “Laravel Where Like” was beneficial to you. You can view EDUCBA’s recommended articles for more information.