Home > Software design >  How to add id if there's custom validation rule when updating the record in Laravel?
How to add id if there's custom validation rule when updating the record in Laravel?

Time:08-28

How to add id in stud_num just like in email and username? the codes found in User Controller.

public function update(Request $request, $id)
    {
        $this->validate($request, [
            'first_name' => 'required|max:255|regex:/^([^0-9]*)$/',
            'middle_name' => 'nullable|max:255|regex:/^([^0-9]*)$/',
            'last_name' => 'required|max:255|regex:/^([^0-9]*)$/',
            'contact' => ['required', 'regex:/^(09|\ 639)\d{9}$/'],
            'course' => 'required',
            'role_as' => 'required',
            'stud_num' => ['required', 'unique:users,stud_num', 'max:15', new StrMustContain('TG')],
            'username' => 'required|alpha_dash|unique:users,username,' . $id,
            'email' => 'required|email:rfc,dns|unique:users,email,' . $id
        ]);
                
        // codes for update    
    }

CodePudding user response:

Just add id like email and username.

'stud_num' => ['required', 'unique:users,stud_num,'.$id, 'max:15', new StrMustContain('TG')]

CodePudding user response:

you can write it like this:

'stud_num'=>['required',Rule::unique('users','stud_num')->ignore($id),'max:15',new StrMustContain('TG')]
  • Related