To return JSON to the controller only return $data;
If JSON to errors, go to the app\Exceptions\Handler.php and look at the render method.
You should be able to overwrite it to look something like this:
public function render($request, Exception $e) {
However, you will need to decide what to do with $e , because it must be an array . You can also set a status code and an array of headers.
But then, with any error, the JSON response is returned.
Edit: It is also useful to note that you can modify the report method to handle how larvel logs the error. More details here .
Duckpuncher
source share