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

相關文章
相關標籤/搜索