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' ]);
}
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)
@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:
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.