You can solve this by adding an accessor in your model:
https://laravel.com/docs/6.x/eloquent-mutators#defining-an-accessor
And return the value as you want.
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Inside my app, I'd say 90% of my models contain image references and at the moment, they are just the filename and extension. E.G my-image.png
However, I want to make it so that on response, instead of just returning;
image: 'my-image.png', it actually returns, image: http://mysite.dev/my-image.png.
Obviously I know I can just loop the eloquent response and do it manually, but since I want to do this for a lot of my models, that seems like a pretty repetitive way to do it.
I want to avoid storing the link directly in the database as well in-case a url change happens in the future.
You can solve this by adding an accessor in your model:
https://laravel.com/docs/6.x/eloquent-mutators#defining-an-accessor
And return the value as you want.
Please or to participate in this conversation.