Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel redirect issue from blade template

I am new in laravel 5 . I am working on larvel page security and i have to prevent open some page or Url but when i use {{ Redirect::to('/dashboard') }} in view it is not working .

Please help me to find a way to 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
like image 816
Aman Kumar Avatar asked Mar 30 '17 12:03

Aman Kumar


People also ask

How do I redirect back in Laravel blade?

In Laravel, you can do something like this: <a href="{{ Request::referrer() }}">Back</a> (assuming you're using Blade).

How do I redirect one page to another in Laravel?

By default laravel will redirect a user to the home page like so: protected $redirectTo = '/home'; To change that default behaviour, add the following code in App/Http/Controllers/Auth/LoginController. php . This will redirect users (after login) to where ever you want.

How do I add a controller in Laravel?

Open the command prompt or terminal based on the operating system you are using and type the following command to create controller using the Artisan CLI (Command Line Interface). Replace the <controller-name> with the name of your controller. This will create a plain constructor as we are passing the argument — plain.


2 Answers

Use a JavaScript redirect instead:

@if(Auth::user()->role_id == 1)
  {{ 'Page' }}
@else 
  <script>window.location = "/dashboard";</script>
@endif
like image 98
joshua miller Avatar answered Sep 24 '22 13:09

joshua miller


@if(your conditions)
    @php
        header("Location: " . URL::to('/'), true, 302);
        exit();
    @endphp
@endif
like image 41
koorosh safeashrafi Avatar answered Sep 22 '22 13:09

koorosh safeashrafi