[FIXED] How to add an key for parent::toArray in Laravel Resources

Issue

My code is;

$live_events = LiveEventContent::whereIn('parent_content_id',$contents_ids)->get();

foreach ($live_events as $live_event)
{
    // add the key isStreamAvailable to the object
    $live_event->isStreamAvailable = true;
}

return LiveEventResource::collection($live_events);
 

and My Resource model;

Public function toArray($request)
{
    returns parent::toArray($request);
}
 

I want to add the isStreamAvailable key to my resource.

How to edit function in resource model instead of parent::toArray

Solution

Add it to returned value from parent:

public function toArray($request)
{
    $response = parent::toArray($request);

    $response['isStreamAvailable'] = true;

    return $response;
}

Answered By – Justinas

Answer Checked By – David Goodson (Easybugfix Volunteer)

Leave a Reply

(*) Required, Your email will not be published