Skip to content
Carl Olsen edited this page Feb 23, 2017 · 2 revisions

Get users with ability

// add to Users model
public function scopeWhereCan($query, $ability)
{
    $query->where(function ($query) use ($ability) {
        // direct
        $query->whereHas('abilities', function ($query) use ($ability) {
            $query->byName($ability);
        });
        // through roles
        $query->orWhereHas('roles', function ($query) use ($ability) {
             $query->whereHas('abilities', function ($query) use ($ability) {
                 $query->byName($ability);
             });
         });
     });
}

$users = User::whereCan($ability);
Clone this wiki locally