0

I am trying to evaluate an array of roles for an auth user. 100 and 102 are the role values I want to check. If the Auth user has one of these then return true. Is this possible? Here is my code so far:

if (Auth::user()->role_id == ([100, 102]) {
//process code here. A lot of code. 
}

I don't wish to repeat and check one at a time as the processing code is a lot and will make file lengthy.

Paulie-C
  • 1,674
  • 1
  • 13
  • 29
Eden WebStudio
  • 687
  • 2
  • 14
  • 34

2 Answers2

2

in_array() will definitely work for you:

if (in_array(auth()->user()->role_id, [100, 102]))

In this case, you could also define a global helper to check if current user belongs to some role or role group:

if (! function_exists('isAdmin')) {
    function isAdmin()
    {
        return in_array(auth()->user()->role_id, [100, 102]);
    }
}

Then you'll be able to use this helper in controllers, models, custom classes etc:

if (isAdmin())

And even in Blade views:

@if (isAdmin())
Alexey Mezenin
  • 158,981
  • 26
  • 290
  • 279
1
As hassan said you can use in_array()

$a= Auth::user()->role_id;
$b= in_array(100, $your_array);
$c= in_array(102, $your_array);

if ( $a == $b && $a == $c ) {
  //process code here. A lot of code. 
}
Bullet
  • 86
  • 12