Here is my code. I am facing this error in Laravel REST API. Just I want to convert Laravel Blade to Laravel REST API. This code is working with Laravel Blade. I am trying to do it with Postman. But here this code is not working.
public function config()
{
try {
$data = [
"ip" => '172.16.1.',
"shade" => [
"one" => [
"from" => 0,
"to" => 63,
],
"two" => [
"from" => 64,
"to" => 127,
],
"three" => [
"from" => 128,
"to" => 192,
],
"four" => [
"from" => 193,
"to" => 257,
],
"all" => [
"from" => 0,
"to" => 257,
],
],
];
return response()->json([ 'status' => true,
'message' => "Configuration Done",
'data' => $data
], 200);
} catch (Exception $e) {
// something went wrong while processing the request
return response()->json([
'error' => $e->getMessage()
], 500);
}
}
public function getShade(Request $request){
try {
$config = $this->config();
$base = $config["ip"];
$curent_shade = $config['shade'][$request->shade];
$from = $curent_shade['from'];
$to = $curent_shade['to'];
$ips_range = range($curent_shade['from'], $curent_shade['to']);
$ips = [];
foreach ($ips_range as $key => $value) {
$ips[] = [
"ip" => $base . $value,
"mn" => $from + 1,
];
$from++;
}
$data['ips'] = $ips;
return response()->json([
'data' => $data,
'status' => true,
'message' => "Sahde Selected Done"
], 200);
} catch (Exception $e) {
// something went wrong while processing the request
return response()->json([
'error' => $e->getMessage()
], 500);
}
}
2
Answers
Your config function returns an
IlluminateHttpResponse
not an array, if it is not requested directly by the api just return the array, or insert your $data array in your getShade function.Your
config()
method returns aresponse()
, which is a full HTTP response. But thegetShade()
method treats that response as an array – that won’t work, it isn’t an array.You could get around this by having your
config()
method simply return an array:Though if
config()
is really intended to return responses to browsers as well as other internal methods which will expect arrays, you’ll need some logic to work out what kind of those responses to return.