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

Khin Zin Zin Thinn's avatar

Laravel returning 500 (Internal Server Error) instead of 422 Unprocessable Entity on Validation fail (API)

Laravel is returning 500 (Internal Server Error) instead of 422 Unprocessable Entity on Validation fail (API).

Store method inside UserController

public function store(Request $request)
    {
        $request->validate([
            'first_name' => 'required|max:50',
            'last_name' => 'required|max:50',
            'email' => 'required|email|max:50',
            'phone_no' => 'required|string|max:20',
            'password' => 'required|string',
        ]);

        User::create([
            'first_name' => $request->first_name,
            'last_name' => $request->last_name,
            'email' => $request->email,
            'phone_no' => $request->phone_no,
            'password' => Hash::make($request->password),
        ]);

        return response()->json(['message' => 'it\'s successful'], 200);
    }

Response object received by axios (nextjs) frontend

config: ...,
data: ...,
headers: ...,
request: ...,
status: 500,
statusText: 'Internal Server Error
0 likes
12 replies
AungHtetPaing__'s avatar

@khin zin zin thinn I am not sure why it is returning 500 but if you need 422 try to manually return it.

public function store(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'first_name' => 'required|max:50',
            'last_name' => 'required|max:50',
            'email' => 'required|email|max:50',
            'phone_no' => 'required|string|max:20',
            'password' => 'required|string',
        ]);

		if($validator->fails()) {
			return response()->json([
				'message' => 'Validation Failed',
				'errors' => $validator->errors(),
			], 422);
		}

        User::create([
            'first_name' => $request->first_name,
            'last_name' => $request->last_name,
            'email' => $request->email,
            'phone_no' => $request->phone_no,
            'password' => Hash::make($request->password),
        ]);

        return response()->json(['message' => 'it\'s successful']); // 200 is default status code
    }
AungHtetPaing__'s avatar

@Khin Zin Zin Thinn If so, it may not be validation error. Check the request arrived to controller or not. Laravel return 422 status to axios for validation error in my project.

public function store(Request $request)
    {
		dd('hit');
        $validator = Validator::make($request->all(), [
            'first_name' => 'required|max:50',
            'last_name' => 'required|max:50',
            'email' => 'required|email|max:50',
            'phone_no' => 'required|string|max:20',
            'password' => 'required|string',
        ]);
		dd('checkPassValidationOrNOt');
		if($validator->fails()) {
			return response()->json([
				'message' => 'Validation Failed',
				'errors' => $validator->errors(),
			], 422);
		}

        User::create([
            'first_name' => $request->first_name,
            'last_name' => $request->last_name,
            'email' => $request->email,
            'phone_no' => $request->phone_no,
            'password' => Hash::make($request->password),
        ]);

        return response()->json(['message' => 'it\'s successful']);
    }
Khin Zin Zin Thinn's avatar

@AungHtetPaing__ The request arrives to the controller. If there is no validation error, creating the user is successful and properly added the user on database. Only the validation is not return the proper error at the moment.

I tried to dd() on the store() method as you mentioned but it shows me cors error even though it was working fine without dd(). I removed the dd() method and tried again adding user and it works. But validation not working yet. Please leet me know if there is a another way I could troubleshoot.

This project is built on Laravel Breeze / Sanctum with access token. Consuming the data from NextJs with Axios.

mahbubrn's avatar

May be error comes from database. Check your database column structure.

Khin Zin Zin Thinn's avatar

@mahbubrn Could you please elaborate more on this? I would like to know which columns or how I should check based on what.

mahbubrn's avatar

@Khin Zin Zin Thinn check your 'users' table columns structures and form data which are you sending. I think the problem is here.

example:

  1. check 'first_name', 'last_name', 'email', 'phone_no' column what data type here
  2. what data are sending from form request
  3. and check is this nullable or not.
  4. is new inserting data unique or not
1 like
Ben Taylor's avatar

Check your laravel.log file to see what the error is. Post the error here please

1 like
Khin Zin Zin Thinn's avatar

@Ben Taylor Hello, this is the error log I received on laravel.log

[2022-11-16 09:24:53] local.ERROR: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '[email protected]' for key 'users_email_unique' (SQL: insert into `users` (`member_no`, `first_name`, `last_name`, `email`, `phone_no`, `whatsapp_no`, `equipment_size`, `gender`, `birthday`, `nationality`, `password`, `updated_at`, `created_at`) values (1749629055860818, ABC, DEF, [email protected], 0987654321, 0123456789, S, Male, 2012-11-16T05:23:55.434Z, AI, y$ql/m.mGcvs1QVE7WHyLYheDv050Qej/WiHyRa14ekgEvB6DQ4cbsi, 2022-11-16 09:24:53, 2022-11-16 09:24:53)) {"userId":1,"exception":"[object] (Illuminate\Database\QueryException(code: 23000): SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '[email protected]' for key 'users_email_unique' (SQL: insert into `users` (`member_no`, `first_name`, `last_name`, `email`, `phone_no`, `whatsapp_no`, `equipment_size`, `gender`, `birthday`, `nationality`, `password`, `updated_at`, `created_at`) values (1749629055860818, ABC, DEF, [email protected], 0987654321, 0123456789, S, Male, 2012-11-16T05:23:55.434Z, AI, y$ql/m.mGcvs1QVE7WHyLYheDv050Qej/WiHyRa14ekgEvB6DQ4cbsi, 2022-11-16 09:24:53, 2022-11-16 09:24:53)) at /Users/macuser/projects/Development/Laravel/project-backend/vendor/laravel/framework/src/Illuminate/Database/Connection.php:760)
AungHtetPaing__'s avatar

@Khin Zin Zin Thinn did you set email column as unique? The error say you are entering duplicate email. If you set your email column as unique, you should also check unique validation in controller.

1 like

Please or to participate in this conversation.