Previous route name in Laravel 5.1-5.8
Simply you can do this to achieve it. I hope it helps
In Controller:
$url = url()->previous();
$route = app('router')->getRoutes($url)->match(app('request')->create($url))->getName();
if($route == 'RouteName') {
//Do required things
}
In blade file
@php
$url = url()->previous();
$route = app('router')->getRoutes($url)->match(app('request')->create($url))->getName();
@endphp
@if($route == 'RouteName')
//Do one task
@else
// Do another task
@endif
Here what work for me. I find this answer and this question and modify it to work in my case: https://stackoverflow.com/a/36476224/2807381
@if(app('router')->getRoutes()->match(app('request')->create(URL::previous()))->getName() == 'public.contact')
Some text
@endif
Update for 5.8 version by Robert
app('router')->getRoutes()->match(app('request')->create(url()->previous()))->getName()
You can't get route name of previous page, so your options are:
Check previous URL instead of a route name.
Use sessions. First, save route name:
session()->flash('previous-route', Route::current()->getName());
Then check if session has previous-route
:
@if (session()->has(`previous-route`) && session(`previous-route`) == 'contacts')
Display something
@endif
- Use
GET
parameters to pass route name.
If I were you, I'd use sessions or would check previous URL.