Why not pass it in through the constructor and append it inside?
$customArray = ['append' => 'this array with key'];
return new ThreadResource($thread, $customArray);
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
In api resources, How can i append a array outside of resource class ? for example:
class ThreadController extends Controller
{
public function show(Thread $thread)
{
$customArray = ['append' => 'this array with key'];
return new ThreadResource($thread);
}
}
How to concat/merge$customArray to result of ThreadResource class ?
Try this then
return array_merge((new ThreadResource($thread))->toArray(request()), $customArray) ;
Please or to participate in this conversation.