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