How to Pass Multiple Parameter in Laravel Query Scope

Hello Artisan,

In this Laravel tip, the example will show you how to pass multiple parameters in the Laravel scope. You know that Laravel scope receives query parameters. But sometimes we need to pass one more parameter along with the query parameter.

So if you don't know Laravel scope - pass 2 parameters to scope then this example is completely for you. See the below example:

/**
     * @param Eloquent Builder $query
     * 
     * @param Searchable Columns
     */
public function scopeSearch(Builder $query, array $columns)
{
  return $query->when(request()->get('q'),function(Builder $q) use ($columns) {
    $q->whereLike($columns, request()->get('q'));
  });
}

 

Read also: Laravel 9 Route Controller Group Improvements Example

 

Hope this example will help you.

 

Facebook Github
A web enthusiastic, a self-motivated full-stack software engineer from Dhaka, Bangladesh with experience in developing applications using Laravel , React and Vue js
ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT