first commit
This commit is contained in:
76
Modules/Leave/app/Models/Leave.php
Normal file
76
Modules/Leave/app/Models/Leave.php
Normal file
@ -0,0 +1,76 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Leave\Models;
|
||||
|
||||
use App\Models\Scopes\CreatedByScope;
|
||||
use Illuminate\Database\Eloquent\Casts\Attribute;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Modules\Employee\Models\Employee;
|
||||
|
||||
class Leave extends Model
|
||||
{
|
||||
protected $table = 'tbl_leaves';
|
||||
protected $primaryKey = 'leave_id';
|
||||
protected $guarded = [];
|
||||
|
||||
protected $appends = ['status_name'];
|
||||
|
||||
const PROGRESS_STATUS = [
|
||||
1 => 'Pending',
|
||||
2 => 'Approved',
|
||||
3 => 'Rejected',
|
||||
];
|
||||
|
||||
const DURATION = [
|
||||
1 => 'Full Day',
|
||||
2 => 'Half Day',
|
||||
3 => 'Multiple',
|
||||
];
|
||||
|
||||
protected static function booted(): void
|
||||
{
|
||||
static::addGlobalScope(new CreatedByScope);
|
||||
}
|
||||
protected function statusName(): Attribute
|
||||
{
|
||||
return Attribute::make(
|
||||
get: function (mixed $value, array $attributes) {
|
||||
switch ($attributes['status']) {
|
||||
case '1':
|
||||
$color = 'dark';
|
||||
break;
|
||||
case '2':
|
||||
$color = 'success';
|
||||
break;
|
||||
case '3':
|
||||
$color = 'danger';
|
||||
break;
|
||||
default:
|
||||
$color = 'light';
|
||||
break;
|
||||
}
|
||||
|
||||
return collect([
|
||||
'status' => self::PROGRESS_STATUS[$attributes['status']],
|
||||
'color' => $color]);
|
||||
},
|
||||
set: fn($value) => $value,
|
||||
);
|
||||
}
|
||||
|
||||
public function getDuration()
|
||||
{
|
||||
return self::DURATION[$this->duration] ?? null;
|
||||
}
|
||||
|
||||
public function employee()
|
||||
{
|
||||
return $this->belongsTo(Employee::class, 'employee_id');
|
||||
}
|
||||
|
||||
public function leaveType()
|
||||
{
|
||||
return $this->belongsTo(LeaveType::class, 'leave_type_id');
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user