1
正如我們可以讀到here,我們可以聽到雄辯的事件並在AppServiceProvider中使用它。它是這樣的:Laravel - 抓住所有雄辯的請求
public function boot()
{
User::creating(function ($user) {
Log::create(['message' => 'create method']);
});
User::deleting(function ($user) {
Log::create(['message' => 'delete method']);
});
}
我所有的雄辯的車型,我希望在創建和誰創造了它,當它記錄在數據庫中。這意味着我需要複製此代碼片段20次,並且只更改User::creating
部分。
有沒有一種方法,我可以趕上從所有車型雄辯的事件,讓這樣的事情:
public function boot()
{
AllModels::creating(function ($model) { // <--- something like this here?
Log::create([
'message' => 'create method',
'model' => get_class($model) // <--- and then get the class name
]);
AllModels::deleting(function ($user) {
/***/
}
});
}