Laravel: custom http status code not working

2449 views php
-4

Laravel version: 5.5

I am trying to return custom http status code from the laravel controller.

In my controller function I tried both the way mentioned bellow but it's not working.

  1. This one returns error "Method setStatusCode does not exist."

    return response()->setStatusCode(202);
    
  2. This one not throwing error but returning 200 always.

    $response = new Response();
    $response->setStatusCode(202);
    $response->header('custom', 555);
    return $response;`
    

answered question

2 Answers

12

Use it like this:

return response()->json("response content", 202);
//or
return response()->make("response content", 202);

Check https://laravel.com/api/5.0/Illuminate/Routing/ResponseFactory.html for more detailed documentation.

posted this
5

You can use return Response::json(['hello' => $value],201);

posted this

Have an answer?

JD

Please login first before posting an answer.