Suppose I have the models A, B, C and D. Now I want to listen for their created event. Usually, I'd go to my AppServiceProvider and add the events for each model, like this:
public function boot()
{
A::created(function ($a) {
//some code
});
B::created(function ($b) {
//some code
});
C::created(function ($c) {
//some code
});
D::created(function ($d) {
//some code
});
}
The code is the same, and I'd like to make that process faster since I actually have 70++ models. So is there a way to bind a single function to the created event of all my models without having to do it one by one?
Edit: Turns out what I wanted was already in this question, but since it's already been answered I don't want to delete it.
Create a base Model class and Inherit to your models, A, B, C and D. The save method of your base model will fire an event, say, model_create, and you'll be listening for that event only on one place. This way you can handle event for you all of your models.