laravel / ideas

Issues board used for Laravel internals discussions.
939 stars 28 forks source link

Overriding toJson() method on models #1171

Open konstantinrachev opened 6 years ago

konstantinrachev commented 6 years ago

The abillity to map toJson() method in order to match custom JSON object formatting. As it is available when mapping toArray() or the model itself. We are currently offered a way to hide or append values from and to the JSON response, however it is impossible to change the hierarchy of the JSON output and thus work with the function.

My proposal is to add the functionality to fully customize the hierarchy of the toJSON() output.

staudenmeir commented 6 years ago

Did you look at API Resources?

solnsumei commented 4 years ago

The issue is why use an external resource class when one can just override the toJSon or toArray method in the said model. Isn't that an extra overhead?