I'm trying to extend the create() method on a model so that every time you call a create() it does some stuff before insert the row.
For example I have a Balance model, which has fields amount and ending_balance. I want to be able to only specify the amount when create() and then have the ending_balance automatically calculated within the model (ie by looking up the previous ending_balance)
I'd also like to be able to put all the create logic within a DB::transaction() so that I can lock the rows while doing the calculations.
I've added this to my model, but it never hits the dd() when creating.
class Balance extends Model
{
use HasFactory;
public static function create(array $attributes = [])
{
dd($attributes);
// Calculate the ending_balance here
return parent::create($attributes);
}
}