Neb*_*ter 5 php laravel laravel-7 laravel-8
Laravel 7 工厂有afterCreatingState()方法,您可以在其中定义具有特定状态的模型保存到数据库后应该发生的事情。
$factory->afterCreatingState(App\User::class, 'active', function ($user, $faker) {
// ...
});
Run Code Online (Sandbox Code Playgroud)
Laravel 8 工厂没有这个方法,而是只有通用afterCreating()。
public function configure()
{
return $this->afterCreating(function (User $user) {
//
});
}
Run Code Online (Sandbox Code Playgroud)
如何实现这种行为?
Neb*_*ter 15
可以在状态定义方法中定义此行为。
public function active()
{
return $this->state(function (array $attributes) {
return [
'active' => true,
];
})->afterCreating(function (User $user) {
// ...
});
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1347 次 |
最近记录: |