Home > other >  Cannot use object of type Illuminate\Http\JsonResponse as array in laravel rest api
Cannot use object of type Illuminate\Http\JsonResponse as array in laravel rest api

Time:01-10

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);
    }
}

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

CodePudding user response:

Your config function returns an Illuminate\Http\Response 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.

CodePudding user response:

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.

  • Related