Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract statusCode from json response in Laravel

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

like image 245
nclsvh Avatar asked Jul 06 '17 08:07

nclsvh


People also ask

What is response() in laravel?

Basic Response Laravel provides several different ways to return response. Response can be sent either from route or from controller. The basic response that can be sent is simple string as shown in the below sample code. This string will be automatically converted to appropriate HTTP response.

What is JSON in laravel?

It stands for JavaScript Object Notation and it provides a great way to share data between languages and applications. Douglas I'm sure is proud of his baby, and we can thank him for declaring himself a standards body and bringing JSON into existence. Let's learn a little more about JSON in Laravel!


1 Answers

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

like image 148
online Thomas Avatar answered Sep 28 '22 22:09

online Thomas