Ad

Laravel Pagination Is Not Working In Search Page

- 1 answer

I am using laravel 5.1. This is my code,

routes.php

Route::any('users/search', array('as' => 'adminuserssearch', 'uses' => '[email protected]'));

UsersController.php

public function adminSearch(){
    $input = Input::all();
    if(!empty($input)){
        $key = Input::get('key');
        $users =  User::where('users.name', 'like', '%'.$key.'%')
         ->orWhere('users.email', 'like', '%'.$key.'%')
         ->paginate(10);

        return view('admin.users.search', ['users' => $users,'tag' =>$key]);
    }
}

search.blade.php

{!! $users->render() !!} //Use this code for display pagination.

When I search for a user, the url is like,

http://myproject/admin/users/search?key=user

But when I click pagination link the url will be like,

http://myproject/admin/users/search/?page=2

the ?key=user section will be lost from the url. How can I fix this problem ?

Ad

Answer

You should append this param to your pagination links .

{!! $users->appends(['key' => $tag])->render() !!}
Ad
source: stackoverflow.com
Ad