You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In version 10.44.0, we introduced a new feature called Introduce Observe attribute for models to facilitate the retrieval of model observers.
Within the HasEvents trait, there exists a method named bootHasEvents() which retrieves attributes from the model classes and subsequently passes them to the observe() method.
Consider a scenario where a model class (e.g., Post) has dependencies such as User, Comment, etc. When the observe method is invoked, we pass the class name to it, and then we create a new instance of that class using new static.
However, in cases where there is a dependency on a model class, an error is encountered, similar to the following:
Too few arguments to function App\Models\Post::__construct(), 0 passed in /var/www/html/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/HasEvents.php on line 67 and at least 3 expected"
This error arises due to the expectation of constructor arguments not being met, causing issues when attempting to instantiate the model class by new static.
Steps To Reproduce
Check the value of resolveObserveAttributes() and if attributes exist, then call the observe method.
Instead of using new static, create a new instance of models using app(static::class) to properly handle the dependencies. This ensures that the IoC container resolves any dependencies automatically.
public static function bootHasEvents(): void
{
if (!empty($attributes = static::resolveObserveAttributes())) {
static::observe($attributes);
}
}
public static function observe($classes)
{
$instance = app(static::class);
foreach (Arr::wrap($classes) as $class) {
$instance->registerObserver($class);
}
}
The text was updated successfully, but these errors were encountered:
Laravel Version
10.44.0
PHP Version
8.1.4
Database Driver & Version
No response
Description
In version 10.44.0, we introduced a new feature called Introduce Observe attribute for models to facilitate the retrieval of model observers.
Within the
HasEvents
trait, there exists a method namedbootHasEvents()
which retrieves attributes from the model classes and subsequently passes them to theobserve()
method.Consider a scenario where a model class (e.g., Post) has dependencies such as User, Comment, etc. When the observe method is invoked, we pass the class name to it, and then we create a new instance of that class using
new static
.However, in cases where there is a dependency on a model class, an error is encountered, similar to the following:
Too few arguments to function App\Models\Post::__construct(), 0 passed in /var/www/html/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/HasEvents.php on line 67 and at least 3 expected"
This error arises due to the expectation of constructor arguments not being met, causing issues when attempting to instantiate the model class by
new static
.Steps To Reproduce
resolveObserveAttributes()
and if attributes exist, then call the observe method.new static,
create a new instance of models usingapp(static::class)
to properly handle the dependencies. This ensures that the IoC container resolves any dependencies automatically.The text was updated successfully, but these errors were encountered: