Get newly created id inside laravel 'created' event

1.1k Views Asked by At

I am using laravel created event. I want to use newly created id in another table. But when i print created object, i don't get the id property. How can i get newly created id in created event.

This is my code.

public static function booted()
{
    static::created(function ($agentTask)
    {
        echo $agentTask->id;
        print_r($agentTask);
    });
}

echo statement printing null. and when i print $agentTask object, i don't find id anywhere.

1

There are 1 best solutions below

1
On

it should be boot not booted

public static function boot()

booted is used to perform any actions required after the model boots.

while boot is used to bootstrap the model and its traits.

Laravel API DOC: booted() boot()