fredemagi's avatar

Advanced search, based on filters, pagination, and preservation of old input

I'm working on a search function on a website where users can search for other users based on filters like genders, areas, etc. I face several difficulties when coding:

  • I cannot seem to figure out how to combine pagination with the collection, I get back. I make us of https://github.com/spatie/laravel-paginateroute for pagination. The problem is that when I go to page 2, I'm shown the result from page 1 and so on. I have tried out different solutions like: Query for all users in a post method based on the filters, ending with ->paginate(NUMBER). Then Session::put('users') in order to be able to retrieve it from a get method so I can return a view with the collection. I've also tried to pass the request object from the post to the get method, but with no result.

  • I cannot figure out how to preserve old data for the form when visiting page 2, page 3, etc.

Atm, the code looks like:

POST METHOD:

 $users = User::select();

    //Add gender.
    if($request->has('genders'))
    {
        $users->whereIn('gender_id', $request->genders);
    }

    //Get matching users.   
    $users = $users->Paginate(self::paginate);    

 
    //Flash collection to session.
    Session::put('users', $users);

    return Redirect::route('match.show')->withInput();

GET METHOD:

$users = null;

    if(Session::has('users'))
    {
        $users = Session::get('users');
    }

    else
    {

        $users = User::Paginate(self::paginate);
    }

    return View("find-match.show", compact("users"));

Regards, Frederik

0 likes
4 replies
Cronix's avatar

I cannot figure out how to preserve old data for the form when visiting page 2, page 3, etc.

Why not just store/retrieve them from session, like you are with $users?

jlrdw's avatar

Normally in a search, the parameters are passed from page to page in the querystring or via parameters. Of course a querystring can only be so long.

Example forum search:

https://laracasts.com/discuss?q=ajax+upload&page=2

Please or to participate in this conversation.