mercredi 7 avril 2021

Get user that belongs to a specific role in laravel

I have following table for roles:

id |  name      |    user_id

1    Admin            NULL
2    Accountant       1
3    Storekeeper      1

and user table like this:

id | name    | role_id  

1     Admin      1
2     xyz        2
3     xyz        3

I want to retrieve all data from users table that have been created by Admin in my case that have user_id 1.

I'm using this relation:

 public function Role()
    {
        return $this->hasMany('App\Models\Role', 'user_id', 'id');
    }

$users = User::with('Role')->whereHas('Role', function($query) {
            $query->where('user_id', 1);
        })->get();

but this is not returning data from users table it is returning rows from roles table only that have user_id 1.

larar



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire