简体   繁体   中英

How to redirect paginate to current page? - Laravel 5.5

I have set up pagination and it's working correctly. But i am not able to redirect to the current page.

Eg: If i invoke update method on currencies?page=2 . I will redirect to the currencies instead of currencies?page=2 .

Controller - index function

public function index()
{
    $currencies = Currency::paginate(5);
    return view('admin.currencies.index')
            ->withCurrencies($currencies);
}

Controller - Edit function

public function edit($id)
{
    $currency = Currency::findOrFail($id);
    return view('admin.currencies.edit')
            ->withCurrency($currency);
}

Controller - Update function

public function update(Request $request, $id)
{
    $currency = Currency::findOrFail($id);
    $currency->name     =   $request->name;
    $currency->rate     =   $request->rate;
    $currency->save();

    return redirect()->route('currencies.index')->with('message', 'Done');
}

Views

{{ $currencies->links() }}

Check out the documentation here https://laravel.com/docs/5.5/pagination#paginator-instance-methods

You can either keep track of the page number (from referral URL if update is on different page or from query param) and pass that along in your update function like this instead of redirecting to a route.

//$page =  grab page number from the query param here.
return redirect('currencies?page='.$page);

or you can also modify your index controller where you pass the page number as optional param and if null default to page 1 and if present pass that in.

$results->url($page)

Good luck.

In case someone has to do redirection to current, or any page and works with named routes .

Tested: Laravel 5

Some assumptions out of the way.

route:

$this->get('/favourite/{columnSorted?}/{sortOrder?}', 'Favourites@index')->name('favourite.list');

assumed project url in browser:

columnSorted: 'title' sortOrder: desc

http://yoursite.net/favourite/title/desc?page=3

Now, named route redirect to page 3.

As you can see in route above, columnSorted and sortOrder are dynamic (? after param in route, eg: {sortOrder?}).

What it means, is that route can have both, just one or none of them.

If you wish to pass them to param array in route, you can do something like this:

/*prep redirect to, where user was params*/
$routeParams = [];

$q = '?';

if ($request->columnSorted)
{
    $routeParams['columnSorted'] = $request->columnSorted;
}
if ($request->sortOrder)
{
    $routeParams['sortOrder'] = $request->sortOrder;
    $q = ''; 
}
if ($request->page)
{
    $routeParams[] = $q . 'page=' . $request->page;
}

return redirect()->route('favourite.list', $routeParams);

Note above that '$q' parameter.

Last (and only last) route parameter $q must not pass '?', or constructed route from named route will have double '??' looking like:

http://yoursite.net/favourite/title/desc??page=3

... and redirect will fail.

Page number you can get from request:

$request->get('page');

// or 

$request->page

... and pass it to method that will do redirect.

You must add a query string parameter to the route.

return redirect()
    ->route('currencies.index', ['page' => $request->get('page', 1)])
    ->with('message', 'Done');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM