belongsTo(Corporate::class); } public function limitJournals() { return $this->morphMany(LimitJournal::class, 'journalable'); } public function latestLimitJournal() { return $this->morphOne(LimitJournal::class, 'journalable')->latestOfMany(); } public function setCodeAttribute($value) { $this->attributes['code'] = !empty($value) ? $value : Str::upper(Str::random('6')); } public function setStartAttribute($value) { $this->attributes['start'] = !empty($value) ? Carbon::parse($value)->format('Y-m-d') : null; } public function setEndAttribute($value) { $this->attributes['end'] = !empty($value) ? Carbon::parse($value)->format('Y-m-d') : null; } public function getLimitBalanceAttribute() { $journal = $this->latestLimitJournal; return $journal ? $journal->balance : (!empty($this->total_premi) ? $this->total_premi : "0"); } }