Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Response::json() - Laravel 5.1

I am trying to return Response::json('data', $request); however, I am getting an error:

FatalErrorException in ProjectsController.php line 74: Call to undefined method Illuminate\Http\Response::json()

Where is the Response::json() is located? What am I doing wrong?

like image 328
senty Avatar asked Aug 06 '15 20:08

senty


People also ask

What is response JSON in laravel?

JSON response can be sent using the json method. This method will automatically set the Content-Type header to application/json. The json method will automatically convert the array into appropriate json response.

How do I respond to a status code in laravel?

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.

How do I return a view in laravel?

Views may also be returned using the View facade: use Illuminate\Support\Facades\View; return View::make('greeting', ['name' => 'James']); As you can see, the first argument passed to the view helper corresponds to the name of the view file in the resources/views directory.

What is response macros in laravel?

Response macros allow you to define a macro that can be called on the response() function. We will create a new ResponseMacroServiceProvider and define a success and error macros inside the boot function: We also need to add this ServiceProvider to the providers array in config/app.


1 Answers

use the helper function in laravel 5.1 instead:

return response()->json(['name' => 'Abigail', 'state' => 'CA']); 

This will create an instance of \Illuminate\Routing\ResponseFactory. See the phpDocs for possible parameters below:

/** * Return a new JSON response from the application. * * @param string|array $data * @param int $status * @param array $headers * @param int $options * @return \Symfony\Component\HttpFoundation\Response  * @static  */ public static function json($data = array(), $status = 200, $headers = array(), $options = 0){      return \Illuminate\Routing\ResponseFactory::json($data, $status, $headers, $options); } 
like image 55
baao Avatar answered Oct 04 '22 14:10

baao