ycsm
1 week ago

Integrity constraint violation

Posted 1 week ago by ycsm

Hi all.

I'm having a real issue and can't seem to figure it out.

I am trying to add a user, this used to work... However,now I get an error: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '' for key 'users_email_unique' (SQL: insert into users (updated_at, created_at)

        public function store(Request $request)
    {

        if(auth()->user()->hasRole('Centre Admin')){
            $centre_id = auth()->user()->centre_id;
        }else{
            $centre_id = $request->centre_id;
        }

        $this->validate($request, [
                'firstname' => ['required', 'string', 'max:255'],
                'lastname' => ['required', 'string', 'max:255'],
                'gender' => ['sometimes', 'numeric'],
                'year' => ['sometimes','nullable', 'numeric'],
                'form_group' => ['sometimes', 'string', 'nullable'],
                'upn' => ['sometimes', 'string', 'nullable'],
                'sen' => ['sometimes', 'string', 'nullable'],
                'ppm' => ['sometimes', 'nullable','numeric'],
                'fsm' => ['sometimes', 'nullable','numeric'],
                'dob' => ['date'],
                'disability' => ['sometimes', 'nullable', 'string'],
                'centre_id' => ['numeric'],
                'email' => ['required', 'string', 'email', 'max:255', 'unique:users,email'],
                'password' => ['required', 'string', 'min:8', 'confirmed'],
            ],[
                'dob.required' => 'Please enter a date',
                'dob.date' => 'Please enter a valid date',
            ]
        );

        User::create([
            'firstname' => $request->firstname,
            'lastname' => $request->lastname,
            'gender' => $request->gender,
            'year' => @$request->year,
            'form_group' => @$request->form_group,
            'fsm' => @$request->fsm,
            'ppm' => @$request->ppm,
            'sen' => @$request->sen,
            'dob' => $request->dob,
            'upn' => $request->upn,
            'disability' => $request->disability,
            'centre_id' => $request->centre_id,
            'email' => $request->email,
            'api_token' => Str::random(60),
            'password' => Hash::make($request->password)
        ]);

        // assign roles
        if(!empty($request->roles)){
            $roles = $request->roles;
            foreach($roles as $role){
                $user->assignRole($role);
            }
        }

        return redirect()->route('users.index')->with('success', 'User created successfully');
    }

I have checked the database for empty email columns, non exist, no matter what email address I use I get that error.

A note - I can update fine, this works perfectly for emails and all fields

EDIT: If I dump $request-email - the email dumps fine, and I am 100% sure the email is not in the database. I have tied numerous random emails.

Please sign in or create an account to participate in this conversation.