How to join query string in Laravel 4 pagination?

3

How do I join query string in pagination of laravel 4?

For example: I have a data listing and on this same screen I have a search filter. When I send a get to request the filter in the list by url http://localhost/lista-de-produtos?marca=1 , the data is listed as requested, but when I go to page 2 the paginate removes the parameter mark = 1 from the url and adds pag = 2, so the filter does not work.

Follow the query code:

$registros = Produtos::where(function ($query) {
    if (Input::has('nome')) {

        $nome = Input::get('nome');

        $query->where('nome', 'LIKE', "%{$nome}%");

    } 

    if (Input::has('marca')){
        $marcaId = Input::get('marca');

        $query->where('marca_id', '=', $marcaId);
    }

})->paginate(20);
    
asked by anonymous 01.12.2015 / 14:40

1 answer

3

In laravel there is a method called appends , where you pass an array, which will represent the data that will go along with the page attribute in the url.

Example:

User::where('admin', '=', 0)->paginate()->appends(Input::except('page'));

In this case, Input::except will be responsible for adding all of the current query string to your paging.

This is very useful in case of paging searches such as Google.

Instead of excepts, you can also use Input::all() .

Update

In newer versions of Laravel 5, you did not have to do this. But if you need, the logic is the same:

User::where('admin', '=', 0)->paginate()->appends($request->all());
    
01.12.2015 / 14:40