zoukankan      html  css  js  c++  java
  • Laravel 更新数据时在表单请求验证中排除自己,检查指定字段唯一性

    原文地址:https://moell.cn/article/24

    不错的laravel网站

    需求场景
    修改用户信息时,在表单请求验证中排除当前邮箱所在的记录行,并检查邮箱的唯一性。
    
    Laravel版本
    5.2
    
    路由
    backend/user/{user}
    
    实例
    <?php
    namespace AppHttpRequestsBackendUser;
    use AppHttpRequestsRequest;
    class UpdateRequest extends Request
    {
        /**
         * Determine if the user is authorized to make this request.
         *
         * @return bool
         */
        public function authorize()
        {
            return true;
        }
        /**
         * Get the validation rules that apply to the request.
         *
         * @return array
         */
        public function rules()
        {
            $id = $this->route('user'); //获取当前需要排除的id,这里的 user 是 路由 {} 中的参数
            return [
                'email' => "required|email|unique:users,email,".$id,
            ];
        }
    }
    验证说明
    unique:表名,字段,需要排除的ID
    

      

  • 相关阅读:
    mysql问题: alter导致速度慢
    MySQL的mysql_insert_id和LAST_INSERT_ID
    linux动态链接库---一篇讲尽
    jsoncpp第二篇------API
    SVN第二篇-----命令集合
    svn第一篇----入门指南
    数据结构之堆
    SZU4
    SZU1
    SZU2
  • 原文地址:https://www.cnblogs.com/yiweiyihang/p/8574401.html
Copyright © 2011-2022 走看看