Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Rarepyre's avatar

How to make IF Conditional Logged User Roles in Blade Laravel?

here my code

    @if(auth()->user()->role_id == 2 || auth()->user()->role_id == 7)
        <p>RED</p>        
    @elseif(auth()->user()->role_id == 2 || auth()->user()->role_id == 8)
        <p>BLUE</p>
    @endif

my code output is only show "RED" when i logged with role_id == 2

what i need is:

  • When the role_id is 2, the output will be "RED BLUE"
  • When the role_id is 7, the output will be "RED"
  • When the role_id is 8, the output will be "BLUE"
0 likes
6 replies
Sergiu17's avatar
Sergiu17
Best Answer
Level 60
@if(auth()->user()->role_id == 2 || auth()->user()->role_id == 7)
	<p>RED</p>
@endif

@if(auth()->user()->role_id == 2 || auth()->user()->role_id == 8)
	<p>BLUE</p>
@endif
1 like
Sinnbeck's avatar

Or a bit cleaner (still mark the other answer as best as that is spot on from your own)

@if(in_array(auth()->user()->role_id, [2, 8]))
	<p>RED</p>
@endif

@if(in_array(auth()->user()->role_id, [2, 8]))
	<p>BLUE</p>
@endif
2 likes
MichalOravec's avatar

Maybe a little better.

In User model add

public function isRed()
{
    return in_array($this->role_id, [2, 7]);
}

public function isBlue()
{
    return in_array($this->role_id, [2, 8]);
}

Then it could be

@if (auth()->user()->isRed())
    <p>RED</p>
@endif

@if (auth()->user()->isBlue())
    <p>BLUE</p>
@endif
1 like

Please or to participate in this conversation.