86 lines
2.0 KiB
PHP
86 lines
2.0 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\HasOne;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
|
|
class Employee extends Model
|
|
{
|
|
use HasFactory;
|
|
|
|
/**
|
|
* The attributes that are mass assignable.
|
|
*
|
|
* @var array<int, string>
|
|
*/
|
|
protected $fillable = [
|
|
'first_name',
|
|
'last_name',
|
|
'email',
|
|
'phone',
|
|
'job_title',
|
|
'hire_date',
|
|
'active',
|
|
];
|
|
|
|
/**
|
|
* The attributes that should be cast.
|
|
*
|
|
* @var array<string, string>
|
|
*/
|
|
protected $casts = [
|
|
'active' => 'boolean',
|
|
'hire_date' => 'date',
|
|
'created_at' => 'datetime',
|
|
'updated_at' => 'datetime',
|
|
];
|
|
|
|
/**
|
|
* Get the thanatopractitioner associated with the employee.
|
|
*/
|
|
public function thanatopractitioner(): HasOne
|
|
{
|
|
return $this->hasOne(Thanatopractitioner::class);
|
|
}
|
|
|
|
/**
|
|
* Get the full name of the employee.
|
|
*/
|
|
public function getFullNameAttribute(): string
|
|
{
|
|
return $this->first_name . ' ' . $this->last_name;
|
|
}
|
|
|
|
/**
|
|
* Scope a query to only include active employees.
|
|
*/
|
|
public function scopeActive($query)
|
|
{
|
|
return $query->where('active', true);
|
|
}
|
|
|
|
/**
|
|
* Scope a query to only include inactive employees.
|
|
*/
|
|
public function scopeInactive($query)
|
|
{
|
|
return $query->where('active', false);
|
|
}
|
|
|
|
/**
|
|
* Scope a query to search employees.
|
|
*/
|
|
public function scopeSearch($query, string $term)
|
|
{
|
|
return $query->where(function ($q) use ($term) {
|
|
$q->where('first_name', 'like', '%' . $term . '%')
|
|
->orWhere('last_name', 'like', '%' . $term . '%')
|
|
->orWhere('email', 'like', '%' . $term . '%')
|
|
->orWhere('job_title', 'like', '%' . $term . '%');
|
|
});
|
|
}
|
|
}
|