EscolaLMS/Core

View on GitHub
src/Repositories/Criteria/RoleCriterion.php

Summary

Maintainability
A
0 mins
Test Coverage
A
100%
<?php


namespace EscolaLms\Core\Repositories\Criteria;

use Illuminate\Database\Eloquent\Builder;
use Spatie\Permission\Models\Role;

class RoleCriterion extends Criterion
{
    public function __construct(string $role)
    {
        $role = Role::where('name', $role)->firstOrFail();
        parent::__construct(null, $role);
    }

    public function apply(Builder $query): Builder
    {
        return $query->whereHas('roles', fn ($q) => $q->where('role_id', $this->value->getKey()));
    }
}