Extract statusCode from json response in Laravel

nclsvh picture nclsvh · Jul 6, 2017 · Viewed 10.7k times · Source

Working in Laravel 5.4

After deleting an image (in my ImageRepository) I send a Json response back to my Controller (where I called the ImageRepository). Now I simply want to check what status code I am getting back to further build on that.

return Response::json([
    'error' => false,
    'code'  => 200,
    'message' => 'Image was deleted!'
], 200);

When I receive this response in my Controller and I dd(); it I see this:

JsonResponse {#461 ▼
  #data: "{"error":false,"code":200,"message":"Image was deleted!"}"
  #callback: null
  #encodingOptions: 0
  +headers: ResponseHeaderBag {#459 ▶}
  #content: "{"error":false,"code":200,"message":"Image was deleted!"}"
  #version: "1.0"
  #statusCode: 200
  #statusText: "OK"
  #charset: null
  +original: array:3 [▶]
  +exception: null
}

I only need to extract the statusCode so that I can send the correct notification to the user (image deleted, image not found, imaage ...)

Can't believe I cannot find a solution for this anywhere.
Thanks

Answer

online Thomas picture online Thomas · Jul 6, 2017

https://laravel.com/api/5.8/Illuminate/Http/RedirectResponse.html

$response->status(); 

Get the status code for the response.

It will return the status code