How to reuse functions using services
Hello.
I have made a function that I need to call from two different observers, and both in the created and update methods (I have to call it 4 times).
I have tried to have one only function in an functions file and call it from the 4 different places, but I have not succeded. It is just a 10 line function, but there must be a better way to do this either than repeating the code 4 times.
Any ideas of how to call a function which is in an external FUNCTIONS CUSTOM FILE from inside the observers?
I understand that inside the same Observer I can use:
$this->nameOfFunction()
But how to call that from an outer file?
2 Replies
Solution
Sounds like you need a service class or a helper class.
Perfect approach. Thank you.
I created a Service and it is working perfectly.
I'll give my example just for if it helps someone in the future:
Created file (Media is what I want to change):
app\Services\MediaService.php
That file is:
Then, when I need to call that function called recalculaMediaNumExp() I do this:
Added the use at the beginning like this:
call the function like this:
Being careful to consider:
1 - In my case I need no parameters to be passed
2 - As this function is : void, no return is needed/possible
3 - If the function inside which you are calling the recalculaMediaNumExp() is not void you must use return:
This is the perfect approach, I think, because if I need to update some parts of the function I will do it in one place only.
I will change the title to -> How to reuse functions using services
Although I understand this is a Laravel issue and not specifically Filament.
Thanks again.