Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

pintu24aipl's avatar

Duplicate record with same number

I have created a user register API using laravel passport. when I hit the API then it created duplicate records with the same number sometimes.

What am I doing wrong?

Here's my code, if someone can help me.

public function signup(Request $request) {

    $validator = Validator::make($request->all(), [
                'first_name' => 'required',
                'last_name' => 'required',
                'email' => 'required|unique:users,email',
                'phone' => ['required','unique:users', 'min:10', 'max:13'],
    ]);

    if ($validator->fails()) {
        $this->response['message'] = $validator->errors()->first();
        return response()->json($this->response, 400);
    }

$userCheck = User::where('phone', $request['phone'])->first();
    if ($userCheck) {
        $this->response['message'] = "The user already exists with this number";
        return response($this->response, 400);
    }

$user = User::create([
                'first_name' => $request['first_name'],
                'last_name' => $request['last_name'],
                'email' => $request['email'],
                'phone' => $request['phone'],
    ]);

$token = $user->createToken('Api access token')->accessToken;

return (new UserResource($user, $token))->additional([
                'status' => 1,
                'message' => "registerd successfully"
    ]);
}
0 likes
10 replies
laracoft's avatar

@pintu24aipl

How are you calling your API? browser? cURL? POST? GET?

Your signup() looks fine.

It is probably your calling code.

bobbybouwmann's avatar

Do you mean that the phone number is duplicate?

Well, this validation can only handle unique cases based on what's in the database. If both calls with the same number come in at the same time, both calls didn't created a record yet.

You can catch this behavior by also making the phone column in the database unique. This way it should be possible to have two records with the same number ;)

1 like
bugsysha's avatar

Have you tried providing column name to the phone validation?

'phone' => ['required','unique:users,phone', 'min:10', 'max:13'], // you are missing phone in the unique constraint

@bobbybouwmann how was the wedding? :)

bugsysha's avatar

@bobbybouwmann did anyone tell her to what she was saying yes to? :D Awesome man. May the Force be with you two.

Please or to participate in this conversation.