I have multiple model that have same logic oncreated method, can I have one observer for that? Or another approach just to avoid creating multiple Observers for each model?
You can use a bootable trait for this purpose.
<?php
namespace App\Traits;
trait MyModelTrait
{
public static function bootMyModelTrait()
{
static::created(function ($model) {
$model->someField = 'someLogicValue';
});
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With