complete Complete User Request Submission section RAD-1
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Resources\Admin;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
use Carbon\Carbon;
|
||||
|
||||
class PetResource extends JsonResource
|
||||
{
|
||||
|
||||
function calculateAge($birthday) {
|
||||
if (!$birthday) return null;
|
||||
|
||||
$birth = Carbon::parse($birthday);
|
||||
$now = Carbon::now();
|
||||
|
||||
$years = (int) $birth->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<string, mixed>
|
||||
*/
|
||||
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'),
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user