Unyxos
Unyxos

Reputation: 77

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 :)

Upvotes: 3

Views: 1551

Answers (2)

shoieb0101
shoieb0101

Reputation: 1582

Try 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

{{}} is used to echo. You can directly access variable inside @if, like @if($user->admin == 0)

Upvotes: 4

Ravan Scafi
Ravan Scafi

Reputation: 6392

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.

Upvotes: 7

Related Questions