私はcontainer_slots
テーブルを照会し、残りの空いているスロットをすべて返します。生のクエリをリソースインデックスメソッドと組み合わせる
return DB::table('container_slots')
->where('occupied', false)
->join('plate_containers','container_slots.plate_container_id', '=', 'plate_containers.id')
->select('plate_containers.name AS Container', DB::raw('count(container_slots.slot) as no_of_slots'))
->groupBy('plate_container_id')->get();
これはうまくいきます。私が達成しようとしています何
戻り
[
{
"Container": "Test container #1",
"no_of_slots": 3
}
]
とすぐに、ユーザーがページを訪問するように求めされたインデックス()メソッドと組み合わせるsomewhoすることです。
public function index()
{
$plateContainers = PlateContainer::paginate(25);
return response()->success($plateContainers);
}
これを返します。
{
"success": {
"total": 2,
"per_page": 25,
"current_page": 1,
"last_page": 1,
"next_page_url": null,
"prev_page_url": null,
"from": 1,
"to": 2,
"data": [
{
"id": 51,
"name": "Test container #1",
"description": null,
"number_of_slots": 5,
"material": "Glass",
"comment": null,
"plate_container_type_id": 51,
"storage_location_id": 13,
"equipment_status_code_id": 17,
"created_by": 1,
"created_at": "2016-12-30 10:24:08",
"updated_at": "2017-01-04 15:43:05",
"updated_by": 1
},
{
"id": 52,
"name": "Test container #2",
"description": null,
"number_of_slots": null,
"material": "glass",
"comment": null,
"plate_container_type_id": 51,
"storage_location_id": 7,
"equipment_status_code_id": 17,
"created_by": 1,
"created_at": "2017-01-04 09:29:15",
"updated_at": "2017-01-04 09:38:51",
"updated_by": 1
}
]
}
}
どこはdata
アレイでIは、上記のクエリの(オブジェクトごとに)キーと値を持っていると思います。それは可能でしょうか? 私はこれを熱望することを達成しようとしましたが、それを正しくすることができませんでした。
もう少し詳しい情報。
PlateContainerモデル
public function containerSlots()
{
return $this->hasMany('App\Models\ContainerSlot');
}
ContainerSlotモデル
public function plateContainer()
{
return $this->belongsTo('App\Models\PlateContainer');
}
がうまくいけば、あなたのアイデアを得ます。御時間ありがとうございます。
更新番号1:以下の回答に基づいています。
私は提案されたメソッドにtryを渡しました。これは返されたものです。
"data": [
{
"id": 51,
"name": "Test container #1",
"description": null,
"number_of_slots": 5,
"material": "Glass",
"comment": null,
"plate_container_type_id": 51,
"storage_location_id": 13,
"equipment_status_code_id": 17,
"created_by": 1,
"created_at": "2016-12-30 10:24:08",
"updated_at": "2017-01-04 15:43:05",
"updated_by": 1,
"my_count": [
{
"Container": "Test container #1",
"no_of_slots": 4
},
{
"Container": "Test container #3",
"no_of_slots": 3
}
],
だからほぼあります。これだけが、PlateContainer
インスタンスごとに占有されていないすべてのスロットを返します。その考え方は、だけをとし、その空きスロットをplate container
とすることです。したがってこの場合、空きスロットはTest Container #1
で済みます。 Test Container #3
は、次のインスタンスに含める必要があります。それは理にかなっていますか?
私は投稿を更新しました。あなたは一見を持てますか?ありがとう! – user3641381