doy57007 2019-02-17 11:50
浏览 95
已采纳

laravel自定义请求验证ID

hi i have laravel project and im using custom request this is my request code

public function rules()
{
    if($this->method() == 'POST' and $this->ajax())
    {
        return [
            'id' => 'integer|min:1|unique:archive_categorys',
            'archive_category_name' => 'max:50|min:1|unique:archive_categorys',
            'archive_category_id' => 'nullable|integer|min:1|max:'.Archive_category::max('id'),
            'archive_category_max' => 'nullable|integer|min:1|',
            'archive_category_plus_value' => 'nullable|integer|min:1|',
            'image' => 'nullable|image|mimes:jpg,jpeg,gif,png|max:2048',
        ];              
    }
    elseif($this->method() == 'PATCH' )
    {
        return [
            'id' => 'required|min:1|unique:archive_categorys,id,'.$this->id,
            'archive_category_name' => 'required|max:50|min:1|unique:archive_categorys,archive_category_name,'.$this->id,
            'archive_category_id' => 'nullable|min:1|max:'.Archive_category::max('id'),
            'archive_category_max' => 'nullable|integer|min:1|',
            'archive_category_plus_value' => 'nullable|integer|min:1|',
            'image' => 'nullable|image|mimes:jpg,jpeg,gif,png|max:2048',
        ];          
    }
    else
    {
        return false;
    }
}

now my problem with method patch

'id' => 'required|min:1|unique:archive_categorys,id,'.$this->id,

now if i tried to change archive_categorys with id 1 to 2 if there is no archive_categorys with id 2 its will update successfully but if there is anther archive_categorys with id 2 its will pass and get error 1062 Duplicate entry
so how can i check unique value in laravel when update thanks

  • 写回答

1条回答 默认 最新

  • dongyou7292 2019-02-17 12:37
    关注

    Considering that you have POST route creating a new entry, id should not be ideally sent from the frontend but should be added automatically as primary key from MySQL.

    Also in your PATCH route, you should send an id to validate if it exists.

    Please check updated validation :

    public function rules()
    {
        if($this->method() == 'POST' and $this->ajax())
        {
            return [
                'archive_category_name' => 'max:50|min:1|unique:archive_categorys,name',
                'archive_category_max' => 'nullable|integer|min:1',
                'archive_category_plus_value' => 'nullable|integer|min:1',
                'image' => 'nullable|image|mimes:jpg,jpeg,gif,png|max:2048',
            ];              
        }
        elseif($this->method() == 'PATCH' )
        {
            return [
                'id' => 'required|exists:archive_categorys,id,',
                'archive_category_name' => 'required|max:50|min:1|unique:archive_categorys,archive_category_name,'.$this->input('id'),
                'archive_category_max' => 'nullable|integer|min:1',
                'archive_category_plus_value' => 'nullable|integer|min:1',
                'image' => 'nullable|image|mimes:jpg,jpeg,gif,png|max:2048',
            ];          
        }
        return [];
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化