Support the ongoing development of Laravel.io →
posted 10 years ago
Laravel.io
Last updated 2 years ago.
0

if im not mistaken the 1st one change the content-type to json. while 2nd will still be html

Last updated 2 years ago.
0

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();
}
Last updated 2 years ago.
0

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 :-)

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

eschmid72 eschmid72 Joined 4 Apr 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.