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

YentlJacobs's avatar

Define model based on role.

I have a table where I save users. Users can either be a student, teacher, headmaster or a combination of this, I make use of roles to achieve this.

I provide specific models for students, teachers and headmasters with their own queries and so on, but I don't have specific tables for these models.

Currently I determine if a user is a teacher by using the boot() function in the Teacher model (same goes for students and headmaster):

protected static function boot()
    {
        parent::boot();

        //Todo: make use of role package to determine if model is teacher or not.
        static::addGlobalScope('teacher', function (Builder $builder) {
            $builder->whereExists(function ($query) {
                $query->select(DB::raw(1))
                    ->from('teacher_offering')
                    ->whereRaw('teacher_offering.user_id = users.id');
            });
        });
    }

This query works well, but I was wondering if I could simplify this by making use of the Laravel-permission package provided by Spatie.

My questions is: can I replace the query inside the boot() function by checking if the teacher role is assigned to the user?

Can I achieve this with this package or should I stay with this query?

0 likes
0 replies

Please or to participate in this conversation.