In Laravel eloquent model has a magical method called the boot()
method. You can override the default behavior.
Suppose you want to set up some field value at the moment of creating the model in Laravel then you can declare the field’s value in the boot()
method.
class User extends Model { public static function boot() { parent::boot(); static::updating(function($model) { // do somthing here }); } }
For example, if you want to generate the UUID field then use the following example code.
public static function boot() { parent::boot(); self::creating(function ($model) { $model->uuid = (string)Uuid::generate(); }); }
Also Read:
Laravel Eloquent Methods firstOrNew firstOrCreate updateOrCreate
Being Tricky 😉