Laravel redirect issue from blade template

I am new to laravel 5. I am working on larvel page security and I need to prevent any page or url from opening but when I use {{ Redirect::to('/dashboard') }}

it does not work.

Please help me find a way use Redirect / Url

in laravel view (Blade template)

I have already tried :- 

1. {{ url('/dashboard') }}
2.  {{ Redirect::to('/dashboard') }}

Code :- 
@if(Auth::user()->role_id == 1)
{{ 'Page' }}
@else 
{{ Redirect::to('/dashboard') }}
@endif

      

Thanks everyone

+7


source to share


4 answers


Use JavaScript redirection instead:



@if(Auth::user()->role_id == 1)
  {{ 'Page' }}
@else 
  <script>window.location = "/dashboard";</script>
@endif

      

+11


source


Your question is difficult to understand, but I suppose you want to redirect the user in the view after checking if it is a login. I have bad news for you, the logic goes on the controller, not the views .

This means you need to check if the user is a login on the controller and after that you redirect to the correct view, on Laravel the most common way to do this is to create a middleware that checks if the user is a login and then apply the correct logic ...



You have many ways to do this, but first you have to understand how it works, you can check it out in the Laravel Authentication documentation.

+5


source


@if(put_your_condition_here)
  {{session(['must_login'=>'must_login'])}};//if you want to display flash message
 window.location.href = "{{url('put your route here')}}";
@endif  

      

0


source


@if(your conditions)
    @php
        header("Location: " . URL::to('/'), true, 302);
        exit();
    @endphp
@endif

      

0


source







All Articles