src/Laravel/Model/ExtTrait.php
private static function registerValidater()
    {
        // 提高优先级，确保在模型观察者之前执行
        $priority = 1;

        // 创建数据前
        static::registerModelEvent('creating', function ($model) {

            if (! $model->rules) return;

            // 针对所有数据进行校验
            $modelRules = $model->rules;
            $rules      = [];
            $messages   = [];

            foreach ($modelRules as $key => $value) {
                // 构造验证规则
                $rules[$key] = implode('|', array_keys($value));

                // 构造错误信息
                array_walk($value, function ($v, $k) use ($key, &$messages) {
                    $k = explode(':', $k);
                    $messages[$key.'.'.$k[0]] = $v;
                });
            }

            // 对请求的数据进行校验
            $input = array_merge($model->attributes, request()->all());
            request()->replace($input);
            $model->validate(request(), $rules, $messages);
            
        }, $priority);

        // 更新数据前
        static::registerModelEvent('updating', function ($model) {

            if (! $model->rules) return;

            // 仅针对脏数据进行校验
            $modelRules = array_intersect_key($model->rules, $model->getDirty());
            $rules      = [];
            $messages   = [];

            foreach ($modelRules as $key => $value) {
                // 构造验证规则
                $rules[$key] = implode('|', array_keys($value));

                // 构造错误信息
                array_walk($value, function ($v, $k) use ($key, &$messages) {
                    $k = explode(':', $k);
                    $messages[$key.'.'.$k[0]] = $v;
                });
            }

            // 对请求的数据进行校验
            $input = array_merge($model->attributes, request()->all());
            request()->replace($input);
            $model->validate(request(), $rules, $messages);

        }, $priority);
    }
private static function registerValidater()
    {
        // 提高优先级，确保在模型观察者之前执行
        $priority = 1;

        // 创建数据前
        static::registerModelEvent('creating', function ($model) {

            if (! $model->rules) return;

            // 针对所有数据进行校验
            $modelRules = $model->rules;
            $rules      = [];
            $messages   = [];

            foreach ($modelRules as $key => $value) {
                // 构造验证规则
                $rules[$key] = implode('|', array_keys($value));

                // 构造错误信息
                array_walk($value, function ($v, $k) use ($key, &$messages) {
                    $k = explode(':', $k);
                    $messages[$key.'.'.$k[0]] = $v;
                });
            }

            // 对请求的数据进行校验
            $input = array_merge($model->attributes, request()->all());
            request()->replace($input);
            $model->validate(request(), $rules, $messages);
            
        }, $priority);

        // 更新数据前
        static::registerModelEvent('updating', function ($model) {

            if (! $model->rules) return;

            // 仅针对脏数据进行校验
            $modelRules = array_intersect_key($model->rules, $model->getDirty());
            $rules      = [];
            $messages   = [];

            foreach ($modelRules as $key => $value) {
                // 构造验证规则
                $rules[$key] = implode('|', array_keys($value));

                // 构造错误信息
                array_walk($value, function ($v, $k) use ($key, &$messages) {
                    $k = explode(':', $k);
                    $messages[$key.'.'.$k[0]] = $v;
                });
            }

            // 对请求的数据进行校验
            $input = array_merge($model->attributes, request()->all());
            request()->replace($input);
            $model->validate(request(), $rules, $messages);

        }, $priority);
    }
注册数据校验

@return void