Laravel - Model boot creating not firing

Viewed 25493

I would like $protected $foo to be assigned 'foo' in the static::creating event, but when I output the final object, the property is null. The event does not seem to fire:

namespace App;

use Illuminate\Database\Eloquent\Model;

class Item extends Model {

    protected $foo;

    public static function boot() {
        parent::boot();
        static::creating(function (Item $item) {
            echo "successfully fired";   //this does not get echoed
            $item->foo = 'foo';
        });
    }
}

What am I doing wrong?

2 Answers

I used this code in laravel 9:

    protected static function booted()
{
    static::creating(function ($model) {
        $model->code = self::generateUniqueCode();
    });
}
Related