Laravel5.1を使っています。
一覧を表示するのにpaginate()でデータを取得して、
結果をJsonで出力したいのです。
php
1$item = Item::where('label', $label) 2->with('user') 3->orderBy('order', 'ASC') 4->paginate(10); 5 6return new JsonResponse($item);
上記のコードだと
json
1{ 2 "total":23, 3 "per_page":10, 4 "current_page":1, 5 "last_page":3, 6 "next_page_url":"http:\/\/localhost\/api\/item?page=2", 7 "prev_page_url":null, 8 "from":1, 9 "to":10, 10 "data":[{ 11 "id":767, 12 "label":"product", 13 "display_order":1, 14 "created_at":"2015-06-04 10:36:32", 15 "updated_at":"2015-06-04 10:36:32", 16 "user":{ 17 "id":953, 18 "user_id":8 19 } 20 }] 21}
このような形で出力されます。(期待通り)
質問ですが、このデータともう一つ出力したいデータがありまして、
php
1return new JsonResponse([ 2 'item' => $item, 3 'another_data' => $another_data 4]);
上記のようにしてみたところ、itemのページネーションデータが出力されなくなってしまいました。
この方法では実現できないのでしょうか?
json
1{ 2 "another_data": "他のデータ", 3 "item": { 4 "total": 23, 5 "per_page": 10, 6 "current_page": 1, 7 "last_page": 3, 8 "next_page_url": "http://localhost/api/item?page=2", 9 "prev_page_url": null, 10 "from": 1, 11 "to": 10, 12 "data": [{ 13 "id": 767, 14 "label": "product", 15 "display_order": 1, 16 "created_at": "2015-06-04 10:36:32", 17 "updated_at": "2015-06-04 10:36:32", 18 "user": { 19 "id": 953, 20 "user_id": 8 21 } 22 }] 23 } 24}
上記のようになることが理想なのですが。
よろしくお願いします。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。