Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return back twice in Laravel?

Tags:

php

laravel

In Laravel, there is a function return back();, which returns the user to the previous page. Is it possible to return back(); more than once within one function to return the user back twice or several times? I tried

public function ....()
{
  return back();
  return back();
}

but it doesn't seem to work.

like image 902
Arthur Tarasov Avatar asked Mar 19 '16 06:03

Arthur Tarasov


2 Answers

No, but you could use session system to save URLs of 2-3-4 pages back. Use Session:: facade or session() helper for shorter syntax:

$links = session()->has('links') ? session('links') : [];
$currentLink = request()->path(); // Getting current URI like 'category/books/'
array_unshift($links, $currentLink); // Putting it in the beginning of links array
session(['links' => $links]); // Saving links array to the session

And to use it:

return redirect(session('links')[2]); // Will redirect 2 links back
like image 141
Alexey Mezenin Avatar answered Nov 15 '22 13:11

Alexey Mezenin


it works for me Redirect::to($request->request->get('http_referrer'))

like image 3
Labb Avdiu Avatar answered Nov 15 '22 11:11

Labb Avdiu