How to search with full name using like in laravel?

by darrion.kuhn , in category: PHP Frameworks , 7 days ago

How to search with full name using like in laravel?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by darion , 7 days ago

@darrion.kuhn 

In Laravel, you can use the whereLike() method to search for records using the LIKE keyword in SQL. Here is an example of how you can search for records by full name using LIKE in Laravel:

1
2
3
$fullName = 'John Doe';

$users = User::whereLike('full_name', '%'.$fullName.'%')->get();


In the above example, User is the model class for the users table, and full_name is the column in the users table that contains the full name of the user. The whereLike() method is a custom macro that you can define in your Laravel application or use a package like Laravel Macroable to extends Laravel's query builder with a custom LIKE search functionality.


By using % before and after the full name, you are telling Laravel to search for any records that contain the full name you provided. This way, you can retrieve records that match the full name partially or exactly.


Make sure to define the whereLike() method in your application before using it or include the necessary package to extend Laravel's query builder with custom functionality.