tutoring/app/Models/User.php

95 lines
1.9 KiB
PHP

<?php
namespace App\Models;
// use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Spatie\Permission\Traits\HasRoles;
class User extends Authenticatable
{
/** @use HasFactory<\Database\Factories\UserFactory> */
use HasFactory, Notifiable;
use HasRoles {
HasRoles::hasRole as spatieHasRole;
}
public function teacherProfile()
{
return $this->hasOne(TeacherProfile::class);
}
public function studentProfile()
{
return $this->hasOne(StudentProfile::class);
}
/**
* The attributes that are mass assignable.
*
* @var list<string>
*/
protected $fillable = [
'first_name',
'last_name',
'email',
'password',
'role',
];
/**
* The attributes that should be hidden for serialization.
*
* @var list<string>
*/
protected $hidden = [
'password',
'remember_token',
];
/**
* Get the attributes that should be cast.
*
* @return array<string, string>
*/
protected function casts(): array
{
return [
'email_verified_at' => 'datetime',
'password' => 'hashed',
];
}
public function lessonProgress()
{
return $this->hasMany(LessonProgress::class);
}
public function courses()
{
return $this->belongsToMany(\App\Models\Course::class, 'course_user')
->withTimestamps();
}
public function hasRole($roles, $guard = null): bool
{
// God inherits all roles
if ($this->roles->pluck('name')->contains('god')) {
return true;
}
// Delegate to Spatie's HasRoles trait
return $this->spatieHasRole($roles, $guard);
}
public function getNameAttribute()
{
return trim($this->first_name . ' ' . $this->last_name);
}
}