Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to conditionally include views in Blade with Laravel?

I'm trying to include some views according to a variable set when defining the view to be rendered.

This is when I render the view:

switch (Auth::user()->role) {
      case 'ADMINISTRADOR':
        return View::make('admin');
        break;

      case 'CAPTURISTA':
        return View::make('capturista');
        break;

      case 'GERENTE DE DEPARTAMENTO':
        return View::make('gerente');
        break;

      case 'SUPERVISOR DE COMPRAS':
        return View::make('supervisor', array('supervisor'=>true));
        break;

      case 'GERENTE ADMINISTRATIVO':
        return View::make('administrativo');
        break;
    }

So I've tried a lot of things at view in order to load some views ONLY if supervisor is true (finally if it was set according to my code).

What I've tried

@if(isset($supervisor))
  @include('includes.boss.supervisor.delivers_modal')
  @include('includes.boss.supervisor.prices_modal')
  @include('includes.boss.supervisor.providers_modal')
@endif

@if({{isset($supervisor)}})
  @include('includes.boss.supervisor.delivers_modal')
  @include('includes.boss.supervisor.prices_modal')
  @include('includes.boss.supervisor.providers_modal')
@endif

<?php if(isset($message)) : ?>
    @section('message')
        <p>hey all</p>
    @endsection
<?php endif; ?>

Also I tried setting the supervisor variable for every view, naturally to false and true.

  @if($supervisor)
          @include('includes.boss.supervisor.delivers_modal')
          @include('includes.boss.supervisor.prices_modal')
          @include('includes.boss.supervisor.providers_modal')
  @endif

None of these worked, how should I achieve it?

* Just in case, I'm using sessions ...

like image 785
diegoaguilar Avatar asked Oct 27 '25 02:10

diegoaguilar


2 Answers

Since Laravel 5.4 you can use @includeWhen .As described in docs:

Including Sub-Views

If you would like to @include a view depending on a given boolean condition, you may use the @includeWhen directive:

@includeWhen($boolean, 'view.name', ['some' => 'data'])
like image 176
Anton Ganichev Avatar answered Oct 28 '25 17:10

Anton Ganichev


Why not, instead of showing a specific view, you make a route for each view, then based on the user role, you redirect him to one of the pages?

switch (Auth::user()->role) {
   case 'ADMINISTRADOR':
     return redirect('administrador');

   case 'CAPTURISTA':
     return redirect('capturista');
...

Then in routes.php:

Route::get('administrador', function() {
   return view('admin');
   // for L4:  View::make('admin');
});

//and one more for each role...

If you're using Laravel 4 you need to use the Redirect facade, something like Redirect::to(..);

In your routes.php file you would make a route for each view. You can filter the routes to make sure the user has the role it tried to access. More on Laravel 4.2 filters here http://laravel.com/docs/4.2/routing#route-filters and here http://laravel.com/docs/4.2/controllers#controller-filters

For Laravel 5, use middlware to check if the user has permission: http://laravel.com/docs/5.0/middleware

like image 27
hfingler Avatar answered Oct 28 '25 16:10

hfingler