#Issue: I cant access a field even though I know for sure its there.
2 messages · Page 1 of 1 (latest)
class BrandResource extends JsonResource
{
public static $wrap = null;
public function toArray($request): array
{
$data = [
'id' => $this->id,
'name' => $this->getTranslations('name'),
'meta_title' => $this->getTranslations('meta_title'),
'meta_description' => $this->getTranslations('meta_description'),
'slug' => $this->getTranslations('slug'),
'description' => $this->getTranslations('description'),
'phone' => $this->phone,
'contact_person' => $this->contact_person,
'working_hours' => $this->working_hours,
'created_at' => HelperFunctions::formatDate($this->created_at),
'categories' => $this->whenLoaded('categories'),
'active' => $this->active,
'is_partner' => $this->is_partner,
'is_cityilove_partner' => $this->is_cityilove_partner,
'is_cityilove_partner_description' => $this->getTranslations('is_cityilove_partner_description'),
'media' => $this->whenLoaded('allMedia', function () {
return MediaResource::collection($this->allMedia);
}),
'default_image' => HelperFunctions::getDefaultPlaceholderImage(),
];
if ($this->relationLoaded('allMedia')) {
$logoImage = $this->allMedia->firstWhere('pivot.media_type', 'logo');
$coverImage = $this->allMedia->firstWhere('pivot.media_type', 'cover');
$coverMobileImage = $this->allMedia->firstWhere('pivot.media_type', 'cover_mobile');
$data['mediaFront'] = [
'logo' => new MediaResource($logoImage),
'cover' => new MediaResource($coverImage),
'cover_mobile' => new MediaResource($coverMobileImage),
];
}
return $data;
}
}