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

ghumaisya's avatar

How to check multiple permission condition in if statement using laravel spatie package?

I need to check condition in if statement for multiple permissions. It is using spatie package from laravel. I use this code below but it seems doesn't work. It can display the output but the output is not correct. It doesn't filter the condition.

 if (auth()->user()->can('View Consultant') || auth()->user()('View Administration') 
 || auth()->user()('View Accountant') || auth()->user()('All departments')) 
   {

     $itemregistrations = DB::table('itemregistrations')
                                            ->where('categoryid','=', '1',',','2',',','3')
                                            ->get();

     return view('profil.index', compact('itemregistrations'));

   } 

Is the code is correct?

The condition is the users with permission (view consultant, view administration, view accountant, all departments) can view list of consultant, administration and accountant from all departments.

For users with permission(view consultant only) can only view consultant list.

0 likes
3 replies
bobbybouwmann's avatar
Level 88

Your where statement is already incorrect. It should be something like this

->whereIn('categoryid', [1, 2, 3])

Also your if statement seems to be incorrect. In the first call you use the can method, but in the other calls you don't? You should fix that first!

1 like
ghumaisya's avatar

Thank you for your correction. I do change the problem code.

Please or to participate in this conversation.