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

hasanhatem's avatar

Laravel API Response status always return 200 in Live Server

Hello,

I have problem in status code in live server

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

This code work fine in localhost i test it. its return 401 Status Code.

after uploaded to cpanel and test. Its return 200 Status code.

this is the hole Api controller :

<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Http\Resources\UserResource;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;

class UserController extends Controller
{
    public function login(Request $request)
    {
        if (request()->has('locale')) {
            $request->validate([
                'locale' => ['nullable', 'string']
            ]);
    
            if (request()->locale !== "" || request()->locale !== null) {
                app()->setLocale(request()->locale);
            } else {
                app()->setLocale('en');
            }
        }

        $validator = Validator::make($request->all(), [
            'login' => ['required', 'string'],
            'password' => ['required', 'string', 'min:8'],
            'device_name' => ['required', 'string'],
        ]);

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

        $user = User::where('email', $request->login)
            ->orWhere('mobile_number', $request->login)
            ->with(['locations', 'defaultLocation', 'cart', 'currency', 'country', 'city'])
            ->first();

        if (! $user || ! Hash::check($request->password, $user->password)) {
            return response()->json([
                'login' => [__('general.auth_failed')],
            ], 401);
        }

        return response()->json([
            'token_key' => $user->createToken($request->device_name)->plainTextToken,
            'user' => new UserResource($user),
        ], 200);
    }

}

i tried to put

->setStatusCode(401);

also not working, i put headers in postman like "Accept", "Content-Type" as "application/json" also not working all the times return 200.

How can i solve this problem.

0 likes
2 replies
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

It looks like you are missing the ->send() method at the end of your response. Try adding it to the end of your response like this:

if ($validator->fails()) {
    return response()->json([
        'errors' => $validator->errors(),
    ], 401)->send();
}

This should ensure that the response is sent with the correct status code.

1 like
hasanhatem's avatar

@LaryAI I search a hole internet i could not find better that this one, WOW Amazing AI tool. Also ChatGPT not give me this answer, Actually I am surprised.

Please or to participate in this conversation.