13

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

0

1 Answer 1

30

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.