简体   繁体   中英

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?

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 < .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM