Search code examples
phplaravelif-statementlaravel-bladelaravel-5.3

Blade if() condition error


I'm trying to write a if() condition with Blade, I have this :

@foreach($users as $user)
        <tr>
            <td>{{$user->id}}</td>
            <td>{{$user->username}}</td>
            <td>{{$user->email}}</td>
            <td><a href="delmember/{{$user->id}}">Supprimer le membre</a></td>
            @if({{$user->admin}} == 0)
                <td><a href="makeadmin/{{$user->id}}">Passer l'utilisateur Admin</a></td>
            @else
                <td><a href="noadmin/{{$user->id}}">Retirer l'admin à l'utilisateur</a></td>
            @endif
        </tr>
@endforeach

I want to check if the user is admin or not (column in my users table) but the if returns me 3 errors : first one : if(^here{{$user->admin}} == 0) -> Expected : condition

Seconde one : if({{$user->admin**^here**}} == 0) -> Expected : semicolon

Third one : if({{$user->admin}}^here == 0) -> Expected : Statement

I searched for a while how to fix it but I don't find, maybe someone could help me.

Thank you :)


Solution

  • Inside blade tags, you don't need to add another tag. You are currently trying to add {{ }} tag inside the @if() tag. Try to think like this: {{ /* php code */ }} and @if(/* php code */).

    So, to fix your problem, you simply write: @if($user->admin == 0) and that should be it.