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; } } }