if im not mistaken the 1st one change the content-type to json. while 2nd will still be html
I have been checking it using curl --head.
It shows: Content-Type: application/json
It works with eloquent queries, too. Just return the result, it will be automatically converted to JSON.
public function anAjaxRequest() {
return User::where('votes', '>', 100)
->select('username','votes')->take(10)->get();
}
That is correct, the framework returns json by default. However i feel like using the json return specifically tells it how to return in case you don't know that json is default. IF they ever change the core, a lot of your application will break or produce unexpected results. So i think although you just can return, return Response::json is very clear on what it does. You're safe as long as they do not change it in the core, but if they do you had wished to use the json in the first place. However YOU are the developer, so you have to decide if you take the risks. Both ways are perfectly fine at this point. I believe there is a quote, wether you thing you are right or wrong, you are always right, makes us human :-)
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community