有没有办法在created_by
不使用观察者的情况下自动填充列,但保持我的控制器清洁?我不想使用观察者,因为当我尝试为我的数据库播种时,会发生错误。
我现在的做法:
class ClientObserver
{
public function creating(Client $client)
{
$client->author()->associate(auth()->user());
}
}
当我注册到服务提供商的启动时,它适用于通过应用程序创建客户端,但是当我使用 artisan 为该工厂的数据库播种时:
$factory->define(Client::class, function (Faker $faker) {
return [
'name' => $faker->company,
'description' => $faker->paragraph,
'address' => $faker->address,
'created_by' => User::all()->random()->id,
];
});
我收到此错误:
Integrity constraint violation: 19 NOT NULL constraint failed: clients.created_by
因为“创建”事件被触发并且观察者行动。是否至少有某种方法可以阻止观察者采取行动?
欢迎任何帮助。谢谢 :)