Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

selcukgiray's avatar

How can I add a custom message to a Laravel API Resource response?

In Laravel, I’m returning an API resource like this:

return (new UserResource($user))
->response()
->setStatusCode(Response::HTTP_CREATED);

This works fine, but I would also like to include a custom message in the JSON response (for example: "message" => "Successfully created").

What is the best way to add such custom fields/messages when returning API resources in Laravel?

0 likes
2 replies
selcukgiray's avatar
return (new UserResource($user))
        ->additional([
            "message" => "Successfully created"
        ])
        ->response()
        ->setStatusCode(Response::HTTP_CREATED);

This is how I solved my problem.

Please or to participate in this conversation.