What are the implications of overriding the toArray() method on an Eloquent model? #33385
-
I have a design construct wherein a model will have a media object associated with it (like a profile picture) which is stored under a certain pathname in an S3 bucket. In MySQL, I simply store the pathname. So in a column called Then, when I need to request that model from the front-end app, I need to attach the appropriate CDN prefix, as well as query parameters to make it a full URL. So I have a method shared by many models called
I really liked this structure a lot, because I can change the CDN whenever I want without having to update MySQL data. And besides, the query parameter changes on every request and necessarily cannot be stored in advance. It is partially based on the user's IP address. The problem is that I ended up calling this Then I had some issues where the I realized it would be much cleaner to override the I tried it out, and it works really well. I basically just request a User and then it signs the image right before converting it to JSON. And I don't have to actually call So my question is this: are there any other times where the Laravel framework uses I used PhpStorm to search for usages, and it turns out there are hundreds. I can't necessarily go through each one to see how it would affect me. OR... would it be better to override a different method such as OR, OR... would it be better to make a macro or otherwise override |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
This has been a monologue but for others stumbling upon my discussion, it seems that there are no adverse implications of toArray(). Basically the only times it seems to be called are when creating a JSON response or when explicitly calling it within your own project code. So I overrode toArray() (instead of my alternative choices like jsonSerialize()) and added image signing, ensuring UTF-8 encoding, and some other things. |
Beta Was this translation helpful? Give feedback.
This has been a monologue but for others stumbling upon my discussion, it seems that there are no adverse implications of toArray(). Basically the only times it seems to be called are when creating a JSON response or when explicitly calling it within your own project code. So I overrode toArray() (instead of my alternative choices like jsonSerialize()) and added image signing, ensuring UTF-8 encoding, and some other things.