ldc-supportweb/app/Models/Schedule.php
2021-12-10 12:18:03 +01:00

65 lines
1.5 KiB
PHP

<?php
namespace App\Models;
use App\Enums\Planning\ActivityType;
use App\Enums\Planning\CellModel;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Kwaadpepper\Enum\Traits\CastsEnums;
class Schedule extends Model
{
use HasFactory;
use CastsEnums;
protected $enumCasts = [
'activity' => ActivityType::class,
'model_lie' => CellModel::class
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'date' => 'date',
];
protected $fillable = [
'user_id',
'date',
'slot',
'activity',
'description',
'id_lie',
'model_lie'
];
public function user()
{
return $this->belongsTo(User::class);
}
//relationship to linked objet
public function relatedModel()
{
switch ($this->model_lie) {
case 'sites':
return $this->belongsTo(Site::class, 'id_lie');
break;
case 'projects':
return $this->belongsTo(Project::class, 'id_lie');
break;
case 'tickets':
return $this->belongsTo(Ticket::class, 'id_lie');
break;
default:
//no model
return new BelongsTo($this->newQuery(), $this, '', '', '');
break;
}
}
}