How do you return an Eloquent model to the browser as JSON? What is the difference between the two methods below? Both seems to work.
#1:
return Response::json($user->toArray());
#2:
return $user->toJson();
The actual data sent is the same, however...
#1 Sends Content-Type:application/json
to the browser
#2 Sends Content-Type:text/html
#1 is more correct but it depends on your Javascript, see: What is the correct JSON content type?
However, it is much simpler to just return the model. It is automagically returned as JSON and the Content-Type is set correctly:
return $model;
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