How to filter many to many structure in Laravel

前端 未结 1 718
心在旅途
心在旅途 2021-01-18 12:57

I got a many to many user and role structure

users
id
name

roles
id
name

role_user

相关标签:
1条回答
  • 2021-01-18 13:54

    This should give you all users who are admins.

    $users = User::whereHas('roles', function($q) {
        $q->where('name', '=', 'admins');
    })->get();
    

    You can see more information on the has() method at http://laravel.com/docs/eloquent#querying-relations

    0 讨论(0)
提交回复
热议问题