Search code examples
laravellaravel-routing

Previous route name in Laravel 5.1-5.8


I try to find name of previous route in Laravel 5.1. With:

{!! URL::previous() !!}

I get the route url, but I try to get route name like I get for current page:

{!! Route::current()->getName() !!}

My client wont a different text for Thank you page, depends on from page (Register page or Contact page) user go to Thank you page. I try with:

{!! Route::previous()->getName() !!}

But that didn't work. I try to get something like:

@if(previous-route == 'contact')
  some text
@else
  other text
@endif

Solution

  • 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()