Probably it's an easy answer, but I don't find it! I'm using Eloquent to perform my queries into the database, having this code:
public function postSearch(Request $request)
{
$query = Customer::select('identification', 'first_name', 'second_name', 'first_lastname', 'second_lastname', 'genre')
->where('identification', 'LIKE', "%$request->identification%")
->where('first_name', 'LIKE', "%$request->first_name%")
->Where('second_name', 'LIKE', "%$request->second_name%")
->Where('first_lastname', 'LIKE', "%$request->first_lastname%")
->Where('second_lastname', 'LIKE', "%$request->second_lastname%")
->Where('genre', 'LIKE', "%$request->genre%");
$request->session()->put('list', $query->get());
$customers = $query->paginate(20);
return view('customer.index', compact('customers'));
}
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index(Request $request)
{
$query = Customer::select('identification', 'first_name', 'second_name', 'first_lastname', 'second_lastname', 'genre');
$request->session()->put('list', $query->get());
$customers = $query->paginate(20);
return view('customer.index', compact('customers'));
}
It works perfectly, but now, I want to optimize it creating another funtion which receives the $query. Something like this:
public function displayResult(Builder $query,Request $request)
{
$request->session()->put('list', $query->get());
$customers = $query->paginate(20);
return view('customer.index', compact('customers'));
}
The objective is to call the function from my previous methods, e.g:
public function index(Request $request)
{
$query = Customer::select('identification', 'first_name', 'second_name', 'first_lastname', 'second_lastname', 'genre');
$this->displayResult($query, $request);
}
But, my view is not displaying nothing. Am I doing something wrong? Should I use a Model instead Builder parameter? If I do so, then it's displaying this error:
Argument 1 passed to App\Http\Controllers\CustomerController::displayResult() must be an instance of App\Http\Controllers\Model, instance of Illuminate\Database\Eloquent\Builder given, called in ....on line 54 and defined
That line is exactly where I call to my new funtion. Thanks in advance.