Pergunta

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

Foi útil?

Solução

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

Outras dicas

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top