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

KirenJames's avatar

Controller Won't Return Defined Status Codes

Hi,

I am stuck with a major issue. I have being developing a application for a while. I had set some responses to send the 422 response error code which was initially working fine, but I recently tested those modules again and now those responses only send a 200 status. I tried setting different status codes and it is always sending 200 status code. I have used request->validate to validate the file type, this also sends a 200 status code in error situations. I checked my current status with my GIT commits and I couldn't find any instance that shows a modification that impedes this. Would really appreciate some help regarding this. I am too far off to roll back to the working state.

If I return status codes from Routes it seems to work fine. (web.php)

My Code That should send a different status code instead of 200

$validated = $request->validate(['inputFile' => 'required|mimes:xlsx,xls']); if($validated){ $exists = DB::table($this->tables['tblperiod'][0])->where([[ $this->tables[ 'tblperiod'][1][1], $request->month ],[ $this->tables['tblperiod'][1][2], $request->year ]])->exists(); if(!$exists){ $this->doExcel($request); $uploadData = $this->getRecentUpload($this->tables['tblallocation'][0], $this->tables['tblallocation'][1], $this->tables['tblperiod'][0], $this->tables['tblperiod'][1], $request->month, $request->year); return response()->json(['success' => 'Data set of '. $request->month .' '. $request->year .' has been successfully imported and ready for reports.', 'upload' => $uploadData, 'month' => $request->month, 'year' => $request->year]); }else{ return response()->json(['errors' => 'The data set ('. $request->month .' '. $request->year .') you are trying to upload already exists! Please check the period and try again.'], 422); } }

0 likes
3 replies
sustained's avatar

The code you are using returns a HTTP 422 as expected both when used in the routes file as well as in a controller action.

Tested using Laravel 5.7.15 and PHP 7.1.23 using:

class TestController extends Controller
{
    public function test422()
    {
        return response()->json([
            'errors' => 'I am a test.'
        ], 422);
    }
}

More information is required.

KirenJames's avatar

Thanks for your reply. That is what is puzzling me too. It was working until my last GIT commit. But when I checked the history and undid (commented) everything up to that state still it was the same. I too checked further (when i var_dumped the status variable in JSONResponse & ResponseFactory classes) it seems that the status gets set to what I assign at the Controller but its becoming 200 somewhere. I am not getting any error to trouble shoot this either. What sort of information do you require?

sustained's avatar

Huh, that's pretty odd. What did that last git commit change?

I'm quite new to PHP and Laravel so I'm not sure if I can help much more than this.

Maybe you're running into some kind of Laravel bug and posting an issue on the Laravel repo might be worthwhile, if you're still stumped at this stage?

Good luck!

Please or to participate in this conversation.