Farzan Najipour
Farzan Najipour

Reputation: 2493

Laravel - Nested if

I want to check if user already logged in and user is female I show specific image otherwise show another image. Here is my code:

@if(Auth::check())

    <a id = "login-popup-title" href="profile" >
      profile

        @if ( {{Auth::User()->gender}}  == "w" )
            <img src="assets/img/profile-f.png">
        @else
            <img src="assets/img/profile-m.png">
        @endif
    </a>
@else
    <a data-toggle="modal" data-target="#regModal">register / login
        <img src="assets/img/lock.png">
    </a>
@endif

In our table, we save user's gender information as w (woman) and m (man).

But I get an error:

FatalErrorException in 33957561dccca78dc8674528d604ac88fdbdaef8.php line 74: syntax error, unexpected '<'

How to do that?

Upvotes: 0

Views: 6479

Answers (1)

Joel Hinz
Joel Hinz

Reputation: 25414

Your problem is in this line:

@if ( {{Auth::User()->gender}}  == "w" )

Since you're already in an if block, you don't need the curly braces - those are only for printing. Do this instead:

@if ( Auth::User()->gender  == "w" )

As it stands, Laravel will convert it to something like this:

<?php if <?php echo Auth::user()->gender; ?> == "w": ?>

And I'm sure you can see why that wouldn't work. :) That's why it's complaining about an unexpected <.

Upvotes: 4

Related Questions