Reputation: 301
I have set up pagination and it's working correctly. But i am not able to redirect to the current page.
E.g: 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() }}
Upvotes: 0
Views: 7933
Reputation: 12574
$results->url($result->currentPage())
Gives you the url to current page
Upvotes: 0
Reputation: 2105
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, e.g.: {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.
Upvotes: 1
Reputation: 9454
You must add a query string parameter to the route.
return redirect()
->route('currencies.index', ['page' => $request->get('page', 1)])
->with('message', 'Done');
Upvotes: 0
Reputation: 889
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.
Upvotes: 1