Why not simply use a scopeon your resource model?
Something like function scopeFilter($query, $filters) {...}, then you could simply pass$request->all() to the scope and dynamically build the query (or even use other scopes).
In your controller you'd then just do something like this:
public function search(Request $request)
{
$data = MyResourceModel::filter( $request->all() );
return view('search', compact('data'));
}
or you could even make it more dynamic by extracting the scopeFilter to a trait filterable and add it to the base model:
trait Filterable
{
public function scopeFilter($query, $filters)
{
// ...
}
}
in your controller:
public function search(Request $request)
{
// Validate that a Resource was given
// Resolve the Resource
$resource = 'Namespace\\To\\Resources' . ucfirst($request->input('resource'));
$data = App::make($resource)::filter( $request->all() );
return view('search', compact('data'));
}
Note: there might be some more elegant ways on the dynamic solution but this one just came up in my mind.
The benefit of using a scope would be, that if no filters are passed it simply returns the same query builder as before adding the scope. So if you'd do something like
Resource::latest()->filter($request->all)->get();
while no filters are given, it will be the same query as if you'd just do:
Resource::latest()->get();