json_decode it first then check if empty.
Aug 6, 2019
2
Level 3
Return an Empty Resource
I am working on implementing the Laravel API Resource to an existing site. One problem I find is that if there are no records the JSON response is null, yet the recource its-self has other coded features like link / meta data.
Controller:
AvailabilityResource::withoutWrapping();
return new AvailabilityResource($profile->availability); // single record hasOne relationship so will never be a collection of records
AvailabilityResource:
public function toArray($request)
{
$profile = \Route::current()->parameter('profile');
return [
'type' => 'availability',
'attributes' => [
'expires_at' => $this->expires_at,
'expires' => $this->expires
],
'meta' => [
'store' => 'availabilityStore'
],
'links' => [
'index' => [
'url' => route('availability.index', ['profile' => $profile]),
'method' => 'get',
],
'update' => [
'url' => route('availability.create', ['profile' => $profile]),
'method' => 'post',
]
],
];
}
But this returns availability: null as a response. No other required data?
Am I missing something?
Thanks,
Dave
Please or to participate in this conversation.