Previous route name in Laravel 5.1-5.8

前端 未结 4 1147
迷失自我
迷失自我 2020-12-10 13:22

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

4条回答
  •  天命终不由人
    2020-12-10 13:41

    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
    

提交回复
热议问题