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?
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.
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.
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.
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.
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); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With