82 lines
1.8 KiB
PHP
Raw Permalink Normal View History

2024-09-03 14:38:34 -07:00
<?php
namespace App\Models;
2024-09-18 16:00:06 -07:00
use Database\Factories\UserFactory;
2024-12-10 15:28:14 -08:00
use Filament\Models\Contracts\HasName;
2024-09-03 14:38:34 -07:00
use Illuminate\Database\Eloquent\Factories\HasFactory;
2025-01-21 21:28:41 -05:00
use Illuminate\Database\Eloquent\Relations\BelongsTo;
2024-09-03 14:38:34 -07:00
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
2024-12-10 15:28:14 -08:00
class User extends Authenticatable implements HasName
2024-09-03 14:38:34 -07:00
{
2024-09-18 16:00:06 -07:00
/** @use HasFactory<UserFactory> */
2024-09-03 14:38:34 -07:00
use HasFactory, Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
2024-12-10 15:28:14 -08:00
'username',
'is_admin',
2024-09-03 14:38:34 -07:00
'password',
2025-01-21 21:28:41 -05:00
'customer_id',
2024-09-03 14:38:34 -07:00
];
/**
* The attributes that should be hidden for serialization.
*
* @var array<int, string>
*/
protected $hidden = [
'password',
'remember_token',
];
/**
* Get the attributes that should be cast.
*
* @return array<string, string>
*/
protected function casts(): array
{
return [
2024-12-10 15:28:14 -08:00
'password' => 'hashed',
2024-09-03 14:38:34 -07:00
];
}
2024-12-10 15:28:14 -08:00
2025-01-21 21:28:41 -05:00
public function setIsAdminAttribute(bool $value): void
{
$this->attributes['is_admin'] = $value;
$this->ensureCustomerIsNotAdmin();
}
public function setCustomerIdAttribute(?int $value): void
{
$this->attributes['customer_id'] = $value;
$this->ensureCustomerIsNotAdmin();
}
public function ensureCustomerIsNotAdmin(): void
{
if ($this->is_admin) {
$this->customer_id = null;
} elseif ($this->customer_id) {
$this->is_admin = false;
}
}
2024-12-10 15:28:14 -08:00
public function getFilamentName(): string
{
return $this->username;
}
2025-01-21 21:28:41 -05:00
public function customer(): BelongsTo
{
return $this->belongsTo(Customer::class);
}
2024-09-03 14:38:34 -07:00
}