You can use the appends method. No need to store data in session.
{{ $users->appends(['sort' => 'votes'])->links() }}
I have a form where I select some options for my query, I'm adding pagination and all works good. Except that I lose the form data when I press "page 2" I'm using Laravel 5.2 Is to keep data on session the only way to keep form state?
It can still work for that. Appends is meant for when your variables are set as url parameters.
mysite.com?name=John&page=3
Here is an example usage:
// users/index.blade.php
<form method="GET">
<input type="text" name="name" value="{{ Input::get('name') }}">
<button type="submit">Filter</button>
</form>
<ul>
@foreach($users as $user)
<li>{{ $user->name }}</li>
@endforeach
</ul>
{{ $users->appends([
'name' => Input::get('name')
])->links() }}
And then the controller could be something like this:
// UsersController.php
public function index(Request $request)
{
$name = request('name');
$users = User::when($name, function ($query) use ($name) {
return $query->where('name', $name);
})->paginate(12);
return view('users.index', compact('users'));
}
Please or to participate in this conversation.