diffInYears($now); $months = (int) $birth->copy()->addYears($years)->diffInMonths($now); $days = (int) $birth->copy()->addYears($years)->addMonths($months)->diffInDays($now); $parts = []; if ($years > 0) $parts[] = $years . ' ساله'; if ($months > 0) $parts[] = $months . ' ماهه'; if ($days > 0 && $years == 0) $parts[] = $days . ' روزه'; return implode(' و ', $parts) ?: 'تازه متولد'; } /** * Transform the resource into an array. * * @return array */ public function toArray(Request $request): array { return [ 'id' => $this->id, 'name' => $this->name, 'type' => $this->type, 'gender' => $this->gender, 'breed' => $this->breed, 'weight' => $this->weight, 'sterilized' => $this->sterilized, 'birthday' => verta($this->birthday)->format('%Y/%m/%d - H:i'), 'age' => $this->calculateAge($this->birthday), 'owner' => $this->relationLoaded('owner') ? $this->owner->only(['id','fullname']) : null, 'created_at' => verta($this->created_at)->format('%Y/%m/%d - H:i'), ]; } }