简体   繁体   中英

When does the booted() method of an Eloquent model get called?

I am trying to add a simple closure-based global query scope:

// Order.php

protected static function booted()
{
    dd('here');
    static::addGlobalScope('test', function ($query) {
        $query->where('amount', '>', 50);
    });
}

However, when I execute Order::toSql() only this SQL statement is generated: "select * from "orders"" , but not the additional constraint that I added in the global query scope.

Also, the greatest debugging tool of all time ( dd() statement) is not being executed, which might be an indication that the booted method is not being called.

So what have I done wrong? I did exactly what the documentation says . When does the booted() method get called?

The boot method name is changed to booted only in the last version of Laravel ( 7.x ), so be aware of this, otherwise it won't work.

Keep in mind always to set the version of Laravel you are using on the documentation (on the top right corner there is a dropdown where you can select the version)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM