I have multiple search functions that all look something like this:
public function searchEntity(Request $request)
{
... // Some variables and other stuff
$q = $request->q;
$entities = Entity::where('name', 'LIKE', '%' . $q . '%')->paginate(15);
$entities->appends(['search' => $q]);
return view(
'entity',
compact('entities', ...)
);
}
Is there a better way to do this where I don't repeat the same code each time I try to search an Eloquent entity or is it better to keep these methods separate?