Home > Net >  Laravel - Paginate function doesn't working on query building using IF
Laravel - Paginate function doesn't working on query building using IF

Time:09-29

I am using paginate() on my Controller to pass data to the View. This is my index function using the paginate()

$userData = User::with('jobUnit')
                        ->select('nip','name','address','phone','email','unit_id')
                        ->paginate(10);

        return view('users.index', [
            'users' => $userData
        ]);

This is the result:

enter image descriptionasdsadsada here

In the other function, I needed to add some IF conditions on the queries that is look like this:

$keyword = $request->keyword;
        
$searchedData = User::with('jobUnit')->select('nip','name','address','phone','email','unit_id');

if ($request->searchFilter == 'nip') {
   $searchedData->where('nip','like','%'.$keyword.'%');
}

$searchedData->paginate(10);

The results is different, which is a problem for me because I am using the pagination links in the View. Here is the results: enter image description here

Does the pagination() not working? Because I tried using the get() as well which should returns "Collection", but it was still returning the "Builder" results.

CodePudding user response:

you need another variable to store the return data

$searchDataPaginated = $searchedData->paginate(10);

or using the current one if you want

$searchedData = $searchedData->paginate(10);

  • Related