Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

MahmoudAdelAli's avatar

Multi Search In Table Laravel

hello , i have a table and there's search above and every thing is good , my client need to advanced search button which this button 'll visible a form with many inputs every input 'll be to column name , example if clients table has

first_name
last_name
email
etc etc

'll be form search above the table to multi filter to get the user is registered between period of time and his name is bla bla and his mail is example@ex... and has a red label , i can make it with GET request but i don't know what if there' empty input how the query 'll be ? and how the eloquent work with that ? and sorry if i cant explain more

0 likes
7 replies
OussamaMater's avatar
Level 37

Yes you can, and the pipeline pattern would do the job perfectly, and keep your code organized for future scaling, if the input is empty, then it will be ignored in the query, the idea is moving your query through pipes, and in each pipe you decide what to do, either add something or remove something based on a condition, in your case if that input is set or no.

This article will get you started: https://dev.to/abrardev99/pipeline-pattern-in-laravel-278p

And actually the code is close to what you need I guess, it will be slight changes.

3 likes
jpmg's avatar

you can do it with Scopes.. for example if you want to do filters with first_name last_name, email, etc, etc etc... you can create a scope function in your model, regardless of whether or not any of them have data. I give you an example..

in your controller.
		$data = $request->only('first_name', 'last_name', 'email');
		$dataquery = ModelName::search()->search($data)->get();

In ModelName you create the function scope.
		public function scopesearch($query, array $filters)
 		{
     			$query->when($filters['first_name'] ?? false, function ($query, $first_name){
          		$query  ->where('first_name','LIKE', '%'.$first_name.'%');
     			});

			$query->when($filters['last_name'] ?? false, function ($query, $last_name){
          	$query ->where('last_name','LIKE', '%'.$last_name.'%');
     		});
		}

In your controller you will receive the collection of data, even if one of them had NO information. $filters['name'] ?? false..

you can read more in https://laravel.com/docs/4.2/eloquent#query-scopes

1 like
MahmoudAdelAli's avatar

@jpmg

idk how to get data inside this anonymous function cause it's so much to store it inside variable for each data

   $data = $request->all();
    $title = "clients";
    $query = Client::query();
    $client_types = ClientType::whereStatus(true)->get();
    $labels = Label::all();
    $flags = Flag::all();

    $query->when($request->has('flag_id'), function ($q, $data) {
      return $q->whereFlagId($data['flag_id']);
    });

    $clients = $query->get();

Please or to participate in this conversation.