Laravel update model with unique validation rule for attribute

后端 未结 18 2115
春和景丽
春和景丽 2020-11-27 12:45

I have a laravel User model which has a unique validation rule on username and email. In my Repository, when I update the model, I rev

相关标签:
18条回答
  • 2020-11-27 13:08

    A simple example for roles update


    // model/User.php
    class User extends Eloquent
    {
    
        public static function rolesUpdate($id)
        {
            return array(
                'username'              => 'required|alpha_dash|unique:users,username,' . $id,
                'email'                 => 'required|email|unique:users,email,'. $id,
                'password'              => 'between:4,11',
            );
        }
    }       
    

    .

    // controllers/UsersControllers.php
    class UsersController extends Controller
    {
    
        public function update($id)
        {
            $user = User::find($id);
            $validation = Validator::make($input, User::rolesUpdate($user->id));
    
            if ($validation->passes())
            {
                $user->update($input);
    
                return Redirect::route('admin.user.show', $id);
            }
    
            return Redirect::route('admin.user.edit', $id)->withInput()->withErrors($validation);
        }
    
    }
    
    0 讨论(0)
  • 2020-11-27 13:08
    public function rules()
    {
        if ($this->method() == 'PUT') {
            $post_id = $this->segment(3);
            $rules = [
                'post_title' => 'required|unique:posts,post_title,' . $post_id
            ];
        } else {
            $rules = [
                'post_title' => 'required|unique:posts,post_title'
            ];
        }
        return $rules;
    }
    
    0 讨论(0)
  • 2020-11-27 13:10

    I am calling different validation classes for Store and Update. In my case I don't want to update every fields, so I have baseRules for common fields for Create and Edit. Add extra validation classes for each. I hope my example is helpful. I am using Laravel 4.

    Model:

    public static $baseRules = array(
        'first_name' => 'required',
        'last_name'  => 'required',
        'description' => 'required',
        'description2' => 'required',
        'phone'  => 'required | numeric',
        'video_link'  => 'required | url',
        'video_title'  => 'required | max:87',
        'video_description'  => 'required',
        'sex' => 'in:M,F,B',
        'title'  => 'required'
    );
    
    public static function validate($data)
    {
        $createRule = static::$baseRules;
        $createRule['email'] = 'required | email | unique:musicians';
        $createRule['band'] = 'required | unique:musicians';
        $createRule['style'] = 'required';
        $createRule['instrument'] = 'required';
        $createRule['myFile'] = 'required | image';
    
        return Validator::make($data, $createRule);
    }
    
    public static function validateUpdate($data, $id)
    {
        $updateRule = static::$baseRules;
        $updateRule['email'] = 'required | email | unique:musicians,email,' . $id;
        $updateRule['band'] = 'required | unique:musicians,band,' . $id;
        return Validator::make($data, $updateRule);
    }
    

    Controller: Store method:

    public function store()
    {
        $myInput = Input::all();
        $validation = Musician::validate($myInput);
        if($validation->fails())
        {
            $key = "errorMusician";
            return Redirect::to('musician/create')
            ->withErrors($validation, 'musicain')
            ->withInput();
        }
    }
    

    Update method:

    public function update($id) 
    {
        $myInput = Input::all();
        $validation = Musician::validateUpdate($myInput, $id);
        if($validation->fails())
        {
            $key = "error";
            $message = $validation->messages();
            return Redirect::to('musician/' . $id)
            ->withErrors($validation, 'musicain')
            ->withInput();
        }
    }
    
    0 讨论(0)
  • 2020-11-27 13:10

    You can trying code bellow

    return [
        'email' => 'required|email|max:255|unique:users,email,' .$this->get('id'),
        'username' => 'required|alpha_dash|max:50|unique:users,username,'.$this->get('id'),
        'password' => 'required|min:6',
        'confirm-password' => 'required|same:password',
    ];
    
    0 讨论(0)
  • 2020-11-27 13:12

    Unique Validation With Different Column ID In Laravel

    'UserEmail'=>"required|email|unique:users,UserEmail,$userID,UserID"
    
    0 讨论(0)
  • 2020-11-27 13:12

    For a custom FormRequest and Laravel 5.7+ you can get the id of your updated model like this:

    public function rules()
        {
            return [
                'name' => 'required|min:5|max:255|unique:schools,name,'.\Request::instance()->id
            ];
        }
    
    0 讨论(0)
提交回复
热议问题