Laravel Response::json() with numeric check Laravel Response::json() with numeric check json json

Laravel Response::json() with numeric check


You can actually pass that option over. If we take a look at the source code for the JsonResponse class you can pass json_encode options as the last parameter.

It would look something like this

return Response::json($properties, 200, [], JSON_NUMERIC_CHECK);

Alternatively you could do this:

    return Response::make(        $properties->toJson(JSON_NUMERIC_CHECK),         200,         ['Content-Type' => 'application/json']    );

Note: if $properties is not an Elequoent model then it must at least implement the JsonableInterface

as well as:

    return Response::make(        json_encode($properties->toArray(), JSON_NUMERIC_CHECK),         200,         ['Content-Type' => 'application/json']    );

The toJson() method in Eloquent just wraps json_encode() and passes it the array of your model. I'd recommend using one of the first two options.


Use method setEncodingOptions of JsonResponse:

return response()->json($properties)->setEncodingOptions(JSON_NUMERIC_CHECK);