mjonat's avatar

Pagination problems...

I'm so close to getting this one I can taste it but unfortunately stuck and cant find anyone else in a similar situation (surprisingly!?).

Right...so I am making a search query...which is working fine and I am getting everything I want but I need to paginate results.

The input for the search form is just 3 drop down select fields and the result has to match all 3...here is the code dealing with the search query and pagination:

function searchAllProfiles(Request $request){
        $query = User::with('spokenLanguages','workingHours','jobType'); //Eager Load User with extras
        $request = $request->all();

        $location = $request['work_location'];
        if($location != ""){
            $results = $query->where('work_location',$location)->paginate(10);
        }

        $position = $request['position'];
        $posResults = JobType::jobs($position)->toArray();
        foreach($posResults as $pos){
            $arrPosResults[] = $pos['user_id'];
        }
        if($position != ""){
            $results = $query->find($arrPosResults);
            $results = new LengthAwarePaginator($results, count($results),10);
        }


        $salary = $request['salary_range'];
        if($salary != ""){
            $results = $query->where('expected_salary',$salary)->paginate(10);
        }

        if(!isset($results)){
            return redirect()->to('/profiles');
        }

        return view('auth.results',compact('results'));
    }

The problem arises with the middle one where I am searching for JobType. Unfortunately I need to search a separate table which is associated with the users table but its not as easy as doing a ->where()

Anyway...what I have works and I still get the results although it doesnt actually paginate...I get the box below the results saying go to page 1/2/3...but all of the results get loaded and it doesnt actually render?

If I don't search for jobtype and just one of the other 2 then it all loads fine and without issue...pagination is working correctly...

Thanks!

0 likes
2 replies
mjonat's avatar

Update: I was looking at the docs and noticed it said I should splice my results so I change the $position part of the function to this:

if($position != ""){
            $results = $query->find($arrPosResults)->all();
            $results = array_slice($results,0,count($results));
            $results = new LengthAwarePaginator($results, count($results),10);
        }

Although I dont see it making any difference? I'm not even sure if i am doing it right haha....

I saw somewhere else people were doing:

return str_replace('/?', '?', $paginator->render());

At the bottom of the function but as I am returning the view and compacting the results this doesnt work for me but I am not sure where to put ->render() if not there? I have just tried $results->render() to no avail.

Please or to participate in this conversation.