Arkhokhar
5 months ago
610
3
Laravel

Resorce file returning array as object

Posted 5 months ago by Arkhokhar

i am trying to get array of order items with sub_menu array inside order items array using laravel resource file.. but it is returning sub_menu as an object not array.

This is Order item resource file return string

 return [
                            'item_id' => $this->item_id,   
                            'item_name' => $this->item_name,
                            'item_price' => '$'.$this->item_price,
                            'image_url' => $this->item_image_url,
                            'item_quantity' => $this->item_quantity,
                            'item_instruction' => $this->item_instruction,
                            'sub_menu' => $sub_menu,
                          ];

and $sub_menu

$sub_menu =[
                                  'sub_item_id' => $sub_menu->id,
                                   'sub_item_name' => $sub_menu->sub_item_name,
                                 'sub_item_price' => '$'.$sub_menu->sub_item_price,
                                          ];

and OUTPUT is

data:[ 
		{
        "item_id": "2",
        "item_name": "some price",
        "item_price": "$some price",
        "image_url": "some url here",
        "item_quantity": "1",
        "item_instruction": "not chilled",
        "sub_menu": {
            "sub_item_id": 1,
            "sub_item_name": "some name",
            "sub_item_price": "$some price"
        }
    }
			]

what output i want

data:[ 
		{
        "item_id": "2",
        "item_name": "some name",
        "item_price": "$some price",
        "image_url": "some url here",
        "item_quantity": "1",
        "item_instruction": "not chilled",
        "sub_menu": [		//see it is array here with [] brackets, not object 
            "sub_item_id": 1,
            "sub_item_name": "some name",
            "sub_item_price": ".50"
        ]
    }
			]

Please sign in or create an account to participate in this conversation.