fix resources hospital

This commit is contained in:
Muhammad Fajar
2022-11-03 15:52:51 +07:00
parent 5f8515feac
commit 6a57b8179a
6 changed files with 62 additions and 75 deletions

View File

@@ -2,8 +2,8 @@
namespace Modules\Linksehat\Transformers\Hospital;
use App\Models\PractitionerRole;
use Illuminate\Http\Resources\Json\JsonResource;
use Modules\Linksehat\Transformers\Speciality\SpecialityForHospitalDetailResource;
class HospitalResource extends JsonResource
{
@@ -15,20 +15,6 @@ class HospitalResource extends JsonResource
*/
public function toArray($request)
{
$querySpecialitys = PractitionerRole::query()
->with(['speciality'])
->where('organization_id', $this->id)
->whereNotNull('speciality_id')
->orderBy('speciality_id')
->groupBy('speciality_id')
->get(['speciality_id']);
foreach ($querySpecialitys as $indexSpeciality => $speciality) {
$specialitys[$indexSpeciality]['id'] = $speciality->speciality->id;
$specialitys[$indexSpeciality]['name'] = $speciality->speciality->name;
$specialitys[$indexSpeciality]['avatar'] = asset('images/default-specialisasi-image.png');
}
return [
'id' => $this->id,
'name' => $this->name,
@@ -45,7 +31,10 @@ class HospitalResource extends JsonResource
'photos' => [
'title' => $this->name,
'photo_url' => url('images/default-hospital-image.png'),
]
],
$this->mergeWhen($this->specialities, [
'specialities' => SpecialityForHospitalDetailResource::collection($this->specialities)
])
];
}
}

View File

@@ -1,45 +0,0 @@
<?php
namespace Modules\Linksehat\Transformers\Hospital;
use App\Models\PractitionerRole;
use Illuminate\Http\Resources\Json\JsonResource;
use Str;
class HospitalResourceDetail extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request
* @return array
*/
public function toArray($request)
{
$querySpecialitys = PractitionerRole::query()
->with(['speciality'])
->where('organization_id', $this->id)
->whereNotNull('speciality_id')
->orderBy('speciality_id')
->groupBy('speciality_id')
->get(['speciality_id']);
foreach ($querySpecialitys as $indexSpeciality => $speciality) {
$specialitys[$indexSpeciality]['id'] = $speciality->speciality->id;
$specialitys[$indexSpeciality]['name'] = $speciality->speciality->name;
$specialitys[$indexSpeciality]['avatar'] = asset('images/default-specialisasi-image.png');
}
return [
'id' => $this->id,
'name' => $this->name,
'address' => $address ?? null,
'distance' => $this->distance ? ($this->distance < 1 ? round($this->distance * 1000, 2) . " m" : round($this->distance, 2) . " km") : null,
'photos' => [
'title' => Str::slug($this->name),
'url' => asset('images/default-hospital-image.png'),
],
'specialitys' => $specialitys
];
}
}

View File

@@ -0,0 +1,24 @@
<?php
namespace Modules\Linksehat\Transformers\Speciality;
use App\Helpers\Helper;
use Illuminate\Http\Resources\Json\JsonResource;
class SpecialityForHospitalDetailResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request
* @return array
*/
public function toArray($request)
{
return [
'id' => $this->id,
'name' => 'Spesialis ' . $this->name,
'avatar' => url('images/default-hospital-image.png'),
];
}
}

View File

@@ -36,7 +36,7 @@ class SpecialityResource extends JsonResource
'specialis' => 'Spesialis ' . (isset($this->speciality->name) ? $this->speciality->name : $this->speciality_name),
'experience' => rand(5, 12),
'rating' => rand(20, 100),
'price' => Helper::currencyIdrFormat($prices) ?? null,
'price' => $prices ? Helper::currencyIdrFormat($prices) : null,
]),
$this->mergeWhen($this->name, [
'title' => 'Spesialis ' . $this->name,