相關(guān)關(guān)鍵詞
關(guān)于我們
最新文章
Laravel學習教程之model validation的使用示例
前言
本文主要給大家介紹了關(guān)于Laravel學習之model validation使用的相關(guān)內(nèi)容,分享出來供大家參考學習,下面話不多說了,來一起看看詳細的介紹吧。
在對database進行寫操作前,需要對數(shù)據(jù)進行validation,如type-check 每一個 model column 的定義('type' 這個column必須是enum('card','loan'))
,這里使用model event來做。
在EventServiceProvider(或自定義一個ValidationServiceProvider)中寫上:
public function boot() { /** * Inspired by @see \Illuminate\Foundation\Providers\FormRequestServiceProvider::boot() * * Note: saving event is always triggered before creating and updating events */ $this->app['events']->listen('eloquent.saving: *', function (string $event_name, array $data): void { /** @var \App\Extensions\Illuminate\Database\Eloquent\Model $object */ $object = $data[0]; $object->validate(); }); }