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

seacucumber's avatar

Using category_id that is assigned to user to show all of the users in that category

Hi, i just started learning laravel and I had this idea that for example, I have a user and my user is under the category of female.

in my user table, i have a category_id and i have a separate table categories to store the category itself.

So i want to show all users under a certain category using the category_id that i have. How do i do it?

Ive tried using this $users = Auth::user()->where('category_id', $category)->get() but no luck. I hope you can help me! Thank you!

0 likes
2 replies
Nakov's avatar

Try directly on the User model, not on an instance of a User. Because Auth::user() returns just the currently authenticated user.

This should work:

use App\Models\User; // this at the top of the class

$users = User::where('category_id', $category)->get();
3 likes
seacucumber's avatar

Thank you! I will try and update later if this will solve my problem!

Please or to participate in this conversation.