I have the name of an image in the database, which I want to convert to a full path using a function that receives the name of the image and assigns it the previous path on the server.
I am doing my queries with eager loading in cascade table1.table2.table3 so it returns the name of the image, not a url, since to obtain the url I must apply a function.
I've looked through the documentation but haven't found anything that can help me.
How can I modify the model so that it always returns a processed field with a function created by me?
If Leonardo's answer doesn't work for you, which was the same thing I initially thought, maybe you're looking for something more versatile. I recommend Fractal
With Fractal you can return your transformed (serialized) data in any way you need, as a collection of some model, but delivered in different ways under different circumstances.
For example, maybe you want that when called from the API, a certain model returns all or some of its attributes but modified from certain methods. What is done is to have "Transformers" for each situation.
For example, you could return your Products with the ProductsToIndexDelApi transformer. Which will return them by transforming their attributes or even adding new attributes, based on the criteria you need.
If this fits what you want, feel free to comment and I can edit the answer with some example in case the documentation doesn't make it very clear.
EDIT An example: File to serialize your Model in a specific way:
Fractal Transformer
In this transformer, I only bring some attributes from my model, in addition, I add an attribute "foliosPorTipo" that does not exist in the model and that is completed according to a method that I created.
To get this, in your Controller , you would have to do something similar to:
In short, in my controller I am asking for a collection to be transformed, to bring it the way I need it by using a transformer for my Model. You could also transform an instance, using Fractal's item method instead of collection...
This is one of several ways that appear in the Fractal documentation, it is the one that suits me, I always add a /Fractal namespace in my models and there I add a base transformer (where sometimes I hide the id with a public id or do not bring it at all) and some other transformers if needed...
I hope the example is of some use to you.
You can use Accessors , to "pre-treat" the output of a property of your model.
For example:
1- In your model (let's say it's called Product) you define the Accessor, assuming that the field in your table is called image:
And that's all, then when you call it from blade it will return the full url to the image.