charlieBrown's avatar

pagination with Laravel using GET

I'm trying to use the paginate method in Laravel but I'm not getting how I'll use paginate when I have parameters passed via GET request. I would like to have a URL like this: public/pesquisar?profissao=Advogado&distrito=Aveiro and still be able to use paginate.

Search Controller

return view('search.mostrarUsuarios', array('profissoesUser' => $profissoesUser));

Html

<div class="well">
    <table class="table">
        <thead>
        <tr>
            <th>First Name</th>
            <th>Last Name</th>
            <th>Link para perfil</th>
            <th style="width: 36px;"></th>
        </tr>
        </thead>
        <tbody>
        @foreach($profissoesUser as $profissaoUser)
            <tr>
                <td>{{ $profissaoUser->users->name }}</td>
                <td>{{ $profissaoUser->users->last_name }}</td>
                <td>{{ $profissaoUser->users->userProfile }}</td>
                </td>
            </tr>
        @endforeach
        </tbody>
    </table>
</div>
<div class="pagination text-center">
    {{ $profissoesUser->links() }}
</div>

If I click to page 2 it leads me to public/pesquisar?page=2 completly forgeting the distritoand profissao parameter. Is there anyway to solve this problem?

0 likes
3 replies
jlrdw's avatar
jlrdw
Best Answer
Level 75

It's in the documentation how to append pagination links. Your parameter arrary gets appended.

2 likes
jlrdw's avatar

Appending To Pagination Links

You may append to the query string of pagination links using the appends method. For example, to append sort=votes to each pagination link, you should make the following call to appends:

{{ $users->appends(['sort' => 'votes'])->links() }} If you wish to append a "hash fragment" to the paginator's URLs, you may use the fragment method. For example, to append #foo to the end of each pagination link, make the following call to the fragment method:

{{ $users->fragment('foo')->links() }}

1 like

Please or to participate in this conversation.