<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                在對database進行寫操作前,需要對數據進行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(); }); } ~~~ 'eloquent.saving: *'是表示listen所有model的saving,即任何一個model的寫操作都會觸發該事件。 然后寫一個abstract model extends EloquentModel: ~~~ // \App\Extensions\Illuminate\Database\Eloquent\Model use Illuminate\Database\Eloquent\Model as EloquentModel; use Illuminate\Validation\ValidationException; abstract class Model extends EloquentModel { public function validate():void { // 1. validate type rules (type-check) $validator = $this->getTypeValidator(); if ($validator->fails()) { throw new ValidationException($validator); } // $validator = $this->getConstraintValidator(); // 2. validate constraint rules (sanity-check) } protected function getTypeValidator() { return $this->getValidationFactory()->make($this->attributes, static::COLUMN_TYPE_RULES); } protected function getValidationFactory() { return app(Factory::class); } protected function getConstraintValidator() { // return $this->getValidationFactory()->make($attributes, static::COLUMN_CONSTRAINT_RULES); } } ~~~ 這樣,在每一個繼承abstract model的子類中,定義const COLUMN_TYPE_RULES就行,如: ~~~ class Account extends Model { public const COLUMN_TYPE_RULES = [ 'id' => 'integer|between:0,4294967295', 'source' => 'nullable|in:schwab,orion,yodlee', 'type' => 'required|in:bank,card,loan', ]; } ~~~ 在寫操作時,提前對每一個 model 的 schema definition進行type-check,避免無效碰撞 database。這個feature的目的是從model schema去校驗輸入數據的字段定義是否合法。 另外一般除了type-check schema definition 外,還得根據業務需要進行邏輯校驗sanity-check constraint rules,如當創建一個account時,輸入inputs里的字段person_id不能是child未成年人,等等。這里業務不同,constraint rules不同,不做過多解釋。這個feature的目的主要是從邏輯上校驗輸入數據的合法性。 OK,總之一般情況下,在寫數據庫前都需要做 model validation,避免無效hit db
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看