how to add where condition on laravel blade?

Published 11 months ago by wardaddy

here my code @if (Auth::user()->where('type_id', 1)) //Admin

showing <a href="#">Add Channel</a>

@elseif (Auth::user()->where('type_id', 2)) //DJ

showing <a href="#">My Channel</a>

@elseif (Auth::user()->where('type_id', 1)) //User

display nothing

@else //Guest

display nothing

What should I do?

Best Answer (As Selected By wardaddy)
Dry7

@wardaddy try

@if (Auth::user()->type_id == 1) //Admin
showing <a href="#">Add Channel</a>
@elseif (Auth::user()->type_id == 2) //DJ
showing <a href="#">Add Channel</a>
@else //Guest
@endif 

adamprickett

You could either check the attribute directly in Blade:

@if (Auth::user()->type_id == 1) // Admin

Or tidy it up and move the logic into your model:

// Model
public function isAdmin()
{
    return $this->type_id == 1;
}

// View
@if (Auth::user()->isAdmin()) // Admin
Dry7
Dry7
11 months ago (151,880 XP)

@wardaddy try

@if (Auth::user()->type_id == 1) //Admin
showing <a href="#">Add Channel</a>
@elseif (Auth::user()->type_id == 2) //DJ
showing <a href="#">Add Channel</a>
@else //Guest
@endif 

tisuchi

You can try this-

@if(Auth::check())
    //detect auth user
    $user = Auth::user();

    @if($user->type_id == 1)
        showing <a href="#">Add Channel</a>
    @endif

    @if($user->type_id == 2)
        showing <a href="#">My Channel</a>
    @endif

    //you can easily remove checing type_id == 3 because you are not showing anything for them.

@else
    //visitor

@endif
wardaddy

Thanks @Dry7 & @tisuchi

Please sign in or create an account to participate in this conversation.