How to make Laravel's Validator $rules optional?

前端 未结 3 1895
南笙
南笙 2021-01-15 04:47

Let\'s say I have User model with two methods:

User.php

class User extends Eloquent
{  
    /* Validation rules */
             


        
3条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-15 05:47

    In stock Laravel, you can call update() on a model, and it won't validate by default, which will give you the desired behavior you described. In the code you posted, you're explicitly overriding the update() method to force validation.

    There are two ways to make the "user" field optional in the code you posted:

    1. Don't set "required" in your $rules for that field.

      'user'  => 'unique:users|alpha_num',
      
    2. Don't override the update() method to force validation before updating.

提交回复
热议问题