Laravel 更新数据时在表单请求验证中排除本身,检查指定字段惟一性

需求场景

修改用户信息时,在表单请求验证中排除当前邮箱所在的记录行,并检查邮箱的惟一性。php

Laravel版本

5.2git

路由

backend/user/{user}github

实例

<?php

namespace App\Http\Requests\Backend\User;

use App\Http\Requests\Request;

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
        return [
            'email' => "required|email|unique:users,email,".$id,
        ];
    }
    
}

验证说明

unique:表名,字段,须要排除的IDapp


欢迎加入技术讨论群 QQ : 339803849
个人开源博客 : https://github.com/moell-peng...ui

相关文章
相关标签/搜索