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

Albertiss's avatar

return response()->json not working

i'm developing an api, in my controller i call a private function that test if user type is 4, else it must send a response message,  the lessage is not returned or executed, when i dd the else i show the dd result, it means it's arrive to else, but can't exexute return response()->json, the private function that i call bellow:
private function convertVariablesOfData($input_data)
    {
        if ($input_data['type'] === 4) {
            $name= $input_data['name'];
        }else {
				return response()->json([
                'response_code' =>506,
                'response_message' => 'user type not valid' ]);
        }
0 likes
8 replies
tykus's avatar

This is inside another method in the Controller; not the action??? In that case, you are returning a Response to the calling method (the Controller action); you give back control to that calling method; you cannot return a Response from there.

Instead, consider throwing an exception (which can then return a response)

CarlEOgden's avatar
private function convertVariablesOfTapTapSend($input_data)
    {
        if ($input_data['type'] == 4) {
            $name= $input_data['name'];
				return response()->json([
                'response_code' =>0,
                'response_message' => 'user type is valid' ]);
        } else {
				return response()->json([
                'response_code' =>506,
                'response_message' => 'user type not valid' ]);
        }

What about that?

tykus's avatar

@CarlEOgden no; for the reason I mentioned already about returning Response (and control) to the calling method.

SilenceBringer's avatar

@CarlEOgden maybe the problem is in your call of the private method? be sure you use return when call


public function yourActionName ()
{
	// ...
	return $this->convertVariablesOfTapTapSend($input_data);
}

tykus's avatar

@SilenceBringer what if the User is valid and the caller should not return at that point.

This is a problem that is easily solved by throwing an appropriate Exception; e.g.

private function convertVariablesOfTapTapSend($input_data)
{
    if ($input_data['type'] == 4) {
        throw new class extends \Exception {
            public function render()
            {
                return response()->json([
                    'response_code' => 506,
                    'response_message' => 'user type not valid'
                ]);
            }
        };
    }
}

Obviously, you can create an actual Exception class that will be reusable @albertiss - this is just by way of example:

SilenceBringer's avatar

@tykus in his code block it returns result for both ways

private function convertVariablesOfTapTapSend($input_data)
    {
        if ($input_data['type'] == 4) {
            $name= $input_data['name'];
				return response()->json([
                'response_code' =>0,
                'response_message' => 'user type is valid' ]);
        } else {
				return response()->json([
                'response_code' =>506,
                'response_message' => 'user type not valid' ]);
        }
Snapey's avatar

you are saying return a json response to the caller (the controller) unless the controller is expecting you to return a response object then its not going to do anything with it.

Please or to participate in this conversation.