Files
ERP-upgrade/app/Models/StaffMember.php
2026-10-13 14:29:40 +00:00

62 lines
1.8 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use App\Traits\LogsActivity;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use App\Models\ErpSystemRole;
class StaffMember extends Authenticatable
{
use HasFactory, Notifiable, LogsActivity;
// 1. Explicitly define the custom table name
protected $table = 'staff_members';
// 2. Define the fillable fields based on your schema
protected $guarded = ['id'];
// 3. Hide the password from array/JSON conversions
protected $hidden = [
'password',
];
public function leaveRequests() {
return $this->hasMany(LeaveRequest::class, 'staff_member_id');
}
public function dependents() {
return $this->hasMany(StaffDependent::class, 'staff_id');
}
public function dailyReports() {
return $this->hasMany(DailyReport::class, 'user_id');
}
public function department(){
return $this->hasOne('App\Models\Department', 'id', 'department_id');
}
// public function designation(){
// return $this->hasOne('App\Models\Department', 'id', 'department_id');
// }
public function roles(): BelongsToMany
{
// Parameter order: Related Model, Pivot Table Name, Foreign Key on Pivot, Related Key on Pivot
return $this->belongsToMany(ErpSystemRole::class, 'user_roles', 'staff_member_id', 'system_role_id');
}
public function hasRole(string $role): bool
{
return $this->roles()->where('name', $role)->exists();
}
// 4. Cast dates correctly
protected $casts = [
// 'dob' => 'date',
'hire_date' => 'datetime',
'password' => 'hashed', // Laravel 10+ password casting
];
}