Support the ongoing development of Laravel.io →
posted 10 years ago
Blade
Last updated 2 years ago.
0

have you tried

@if(isset(Auth::user->id())
//this confirms that the user is logged in using the default Auth users method.
  @extends("template/index")
@else
    @extends("template/login")
@endif

However, this is how I did mine in a project awhile back

        @if (isset(Auth::user()->id))
            @include('layouts.admin')
        @endif

So, if the user was logged in that meant they were an admin and it showed admin controls. Maybe you can just use include rather then extends to include that section based on your credentials.

Hope that helps!

Last updated 2 years ago.
0

I would not do an template control structure within a check like this. What you should do is have a route filter that checks for auth, and if they are not logged in redirect them to the appropriate page that is maintained specifically for that purpose. If the same content is on the un-authed page and authed page and you are only changing the template, then you can @include that content from its own partial.

Last updated 2 years ago.
0

lordmilutin said:

I want do check simple condition and then decide what template should I extend. I want to do something like this:

@if(Auth::check())
  @extends("template/index")
@else
   @extends("template/login")
@endif

But when I do, I get both templates extended ....

I use a ternary operator in the function args to do this, mainly for swapping out logged in vs logged out nav on content only pages that are accessible internally and externally that are the same content, having a whole separate page & route just adds to copy-n-paste maintenance.

So for you it would be :

@extends(Auth::user() ? 'template/index' : 'template/login');
0

I had the same exact issue, and using Auth::user() with ternary operator did the trick. Thanks JeremyHutchings!!

0

JeremyHutchings said:

lordmilutin said:

I want do check simple condition and then decide what template should I extend. I want to do something like this:

@if(Auth::check())
  @extends("template/index")
@else
   @extends("template/login")
@endif

But when I do, I get both templates extended ....

I use a ternary operator in the function args to do this, mainly for swapping out logged in vs logged out nav on content only pages that are accessible internally and externally that are the same content, having a whole separate page & route just adds to copy-n-paste maintenance.

So for you it would be :

@extends(Auth::user() ? 'template/index' : 'template/login');

Works like a charm, Thanks!

0

Laravel Blade template step by step instructions with source code http://phpclicks.com/laravel-blade-template/

Last updated 7 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.