Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

api json response not coming in array form

Tags:

rest

php

laravel

First of all I am new to laravel.

This is my controller

$itemsList=Items::all()->where('shop_id',$request->shop_id);

return response()->json(['data'=>$itemsList]);

This is the response

{
    "data": {
        "10": {
            "id": 11,
            "shop_id": 1,
            "title": "Test",
            "price_nd": 12,
            "price_wd": 10,
            "updated_at": "2019-11-14 00:00:00",
            "created_at": "2019-11-14 00:00:00"
        },
        "11": {
            "id": 12,
            "shop_id": 1,
            "title": "Test",
            "price_nd": 12,
            "price_wd": 10,
            "updated_at": "2019-11-14 00:00:00",
            "created_at": "2019-11-14 00:00:00"
        },
        "14": {
            "id": 15,
            "shop_id": 1,
            "title": "Test",
            "price_nd": 12,
            "price_wd": 10,
            "updated_at": "2019-11-14 00:00:00",
            "created_at": "2019-11-14 00:00:00"
        }
    }
}

The problem is I want to return an array list of the items

I tried many approached but I don't know what is the problem or what I am missing

like image 213
Mohamed Shaheen Avatar asked Aug 05 '26 09:08

Mohamed Shaheen


1 Answers

Try. Use get() method .

get() and all() both get same output but

all() method not useful for use where condition. get() method can useful to use other conditions

$itemsList=Items::where('shop_id',$request->shop_id)->get();

return response()->json(['data'=>$itemsList]);
like image 58
VIKAS KATARIYA Avatar answered Aug 07 '26 00:08

VIKAS KATARIYA



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!