Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

stratboy's avatar

Model::boot() or Model::booted()?

Hi, I need to use some build in model events. In docs and other sources I find both

protected static function boot(){
    parent::boot();

    static::updated(function ($model) {
        //lg('model updated','');
    });
}
protected static function booted(): void{
    static::created(function (User $user) {
        // ...
    });
}

What's the difference and why should I use one or the other?

0 likes
3 replies
LaryAI's avatar
Level 58

In Laravel, both boot and booted methods are used to hook into the model's lifecycle events, but they serve slightly different purposes and are used in different contexts.

boot Method

The boot method is a static method that is called when the model is first initialized. This is where you can register model event listeners. It is important to call parent::boot() to ensure that any booting logic defined in the parent class is also executed.

Example:

protected static function boot()
{
    parent::boot();

    static::updated(function ($model) {
        // Logic to execute when the model is updated
        // lg('model updated','');
    });
}

booted Method

The booted method is also a static method, but it is called after the boot method has been executed. This method is useful if you want to ensure that all booting logic has been completed before executing your custom logic.

Example:

protected static function booted(): void
{
    static::created(function (User $user) {
        // Logic to execute when a new User model is created
        // ...
    });
}

Key Differences

  1. Execution Order:

    • boot is called first when the model is initialized.
    • booted is called after the boot method has completed.
  2. Use Case:

    • Use boot if you need to register event listeners and ensure that the parent class's boot logic is executed.
    • Use booted if you need to perform actions after all booting logic has been completed.

Which One to Use?

  • If you need to register event listeners and ensure that the parent class's boot logic is executed, use the boot method.
  • If you need to perform actions after all booting logic has been completed, use the booted method.

In most cases, boot is sufficient for registering model event listeners. However, if you have specific requirements that need to be executed after all booting logic, then booted is the appropriate choice.

Example Usage

Here is an example that demonstrates the use of both methods in a model:

class User extends Authenticatable
{
    protected static function boot()
    {
        parent::boot();

        static::updated(function ($model) {
            // Logic to execute when the model is updated
            // lg('model updated','');
        });
    }

    protected static function booted(): void
    {
        static::created(function (User $user) {
            // Logic to execute when a new User model is created
            // ...
        });
    }
}

In this example, the updated event listener is registered in the boot method, and the created event listener is registered in the booted method. This ensures that the updated event listener is registered as part of the model's initialization process, while the created event listener is registered after all booting logic has been completed.

vincent15000's avatar

For a better code, you should use the observers.

https://laravel.com/docs/11.x/eloquent#observers
martinbean's avatar
Level 80

@stratboy You should be using booted. It’s automatically called if defined. If you override boot then you run into inheritance issues, and have to be remember to call parent::boot, so Laravel introduced the booted method specifically to avoid those issues.

3 likes

Please or to participate in this conversation.