skip to Main Content

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


  1. 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.

    Login or Signup to reply.
  2. Your config() method returns a response(), which is a full HTTP response. But the getShade() method treats that response as an array – that won’t work, it isn’t an array.

    public function config() {
        // ... your code
        // This returns a full HTTP response for a browser, not an array
        return response()->json([ 'status' => true,
                                  'message' => "Configuration Done",
                                  'data' => $data
        ], 200);
    }
    
    public function getShade(Request $request){
        try {
            // Here $config is a full HTTP request ...
            $config = $this->config();
    
            // ... but you are treating it like an array, won't work.
            $base = $config["ip"];
    

    You could get around this by having your config() method simply return an array:

    public function config() {
        // ... your code
        return [
            'status'  => true,
            'message' => "Configuration Done",
            'data'    => $data
        ];
    

    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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search