User.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. namespace App\Models;
  3. use App\Enums\UserLanguageSource;
  4. use App\Enums\UserTypeSource;
  5. use Illuminate\Database\Eloquent\Factories\HasFactory;
  6. use Illuminate\Foundation\Auth\User as Authenticatable;
  7. use Illuminate\Notifications\Notifiable;
  8. use Laravel\Sanctum\HasApiTokens;
  9. use Illuminate\Database\Eloquent\Relations\BelongsToMany;
  10. class User extends Authenticatable
  11. {
  12. use HasFactory, Notifiable, HasApiTokens;
  13. /**
  14. * The attributes that are mass assignable.
  15. *
  16. * @var array<int, string>
  17. */
  18. protected $fillable = [
  19. 'name',
  20. 'email',
  21. 'password',
  22. ];
  23. /**
  24. * The attributes that should be hidden for serialization.
  25. *
  26. * @var array<int, string>
  27. */
  28. protected $hidden = [
  29. 'password',
  30. 'remember_token',
  31. ];
  32. /**
  33. * Get the attributes that should be cast.
  34. *
  35. * @return array<string, string>
  36. */
  37. protected function casts(): array
  38. {
  39. return [
  40. 'email_verified_at' => 'datetime',
  41. 'password' => 'hashed',
  42. 'type' => UserTypeSource::class,
  43. 'language' => UserLanguageSource::class,
  44. ];
  45. }
  46. public function isAdmin(): bool
  47. {
  48. return $this->type === UserTypeSource::Admin;
  49. }
  50. public function permissions(): BelongsToMany
  51. {
  52. return $this->belongsToMany(Permission::class, 'user_type_permissions', 'user_type', 'permission_id');
  53. }
  54. }