This event would likely fire at the end of __construct and would be helpful to define default values that are dynamic, and need to be set when the model is instantiated. For example, if you have a multitenant application with separate sites you could autopopulate a site_id attribute based on however that is detected (Request header on an API, the domain, etc). For example:
public static function bootSiteContentTrait(): void
{
static::registerModelEvent('constructed', function(Model $model) {
if (Core::site() && !$model->exists && !$model->site_id) {
$model->site_id = Core::site()->id;
}
});
}
This event would likely fire at the end of
__construct
and would be helpful to define default values that are dynamic, and need to be set when the model is instantiated. For example, if you have a multitenant application with separate sites you could autopopulate asite_id
attribute based on however that is detected (Request header on an API, the domain, etc). For example: