How is that possible ? Le's say that in a controller we have something like :

 $something = Something:all();

If I just return $something, an json object is returned with content of table {id:1, title :'etc'}

However if I die dump dd($something), I can see $something is an entire collection.

So the return is not returning the collection, is returning json object...?

有帮助吗?

解决方案

This is the magic of Laravel, when you return the collection it convert it to jsonautomatically, this is an advantage for building an api.

Note that when a model or collection is cast to a string, it will be converted to JSON, meaning you can return Eloquent objects directly from your application's routes!

Check Laravel Documentation (Converting A Model To JSON).

其他提示

Well, in addition to @Sheikh Heera answer, such conversion happens in the Illuminate\Http\Resonse class here, by checking if the response content is "JSONable", which is true for Illuminate\Support\Collection which Illuminate\Database\Eloquent\Collection inherits from.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top