How to create custom pagination link?

2019-09-08 16:03发布

问题:

Right now i have this link on paginate :

www.test.com/search/filter?page=1

www.test.com/search/filter?page=2

and what i want is this: www.test.com/search?search=&page=2 and so on

Right now i have this:

{!! $properties->appends(['toggle' => Request::get('toggle'), 'search' => Request::get('search')])->render() !!}

How can i change this to have route like this what i want?

i found this:

Route::get('users', function () {
    $users = App\User::paginate(15);

    $users->setPath('custom/url');

    //
});

But problem is that i use one function for multiple stuff so i can not set path in controller.

回答1:

You can keep your existing query like this. you can define one function in your controller

public function getExistingQueryParams()
{
    $existingQueryParams = [];

    foreach (request()->all() as $key => $value)
    {
        if ($key != 'page')
        {
            $existingQueryParams[$key] = urldecode($value);
        }
    }

    return $existingQueryParams;
}

In your controller's function which is returning the view call this function.

$existingQuery = $this->getExistingQueryParams();

Pass this variable in your view and in your view you can use it like this

{{ $propertiers->appends($existingQuery)->links() }}