Reputation: 119
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...?
Upvotes: 3
Views: 3399
Reputation: 6223
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.
Upvotes: 0
Reputation: 146229
This is the magic of Laravel
, when you return the collection it convert it to json
automatically, 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
).
Upvotes: 7