Mohamed Shaheen
Mohamed Shaheen

Reputation: 663

api json response not coming in array form

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

Upvotes: 3

Views: 253

Answers (3)

Prathamesh Doke
Prathamesh Doke

Reputation: 845

Try this code..

  $itemsList=Items::where('shop_id',$request->shop_id)->get();
  dd($itemsList); // add this step in your code
  return response(['data' => $itemsList]);

Upvotes: 2

VIKAS KATARIYA
VIKAS KATARIYA

Reputation: 6005

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

Upvotes: 2

Poldo
Poldo

Reputation: 1932

You this approach.

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

Upvotes: 1

Related Questions