Galivan Galivan - 1 year ago 199
JSON Question

Laravel - Return json along with http status code

If I return an object:

return Response::json([
'hello' => $value

the status code will be 200. How can I change it to 201, with a message and send it with the json object?.

I don't know if there is a way to just set the status code in Laravel.

Answer Source

You can use http_response_code() to set HTTP response code.

If you pass no parameters then http_response_code will get the current status code. If you pass a parameter it will set the response code.

http_response_code(201); // Set response status code to 201

For Laravel(Reference from:

return Response::json([
    'hello' => $value
], 201); // Status code here
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download