2017-01-04 4 views
0

私は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は、次のインスタンスに含める必要があります。それは理にかなっていますか?

答えて

0

熱心な読み込みでうまくいくはずです。

$plateContainers = PlateContainer::with('ContainerSlot')->paginate(25); 

これにより、すべてのPlateContainerにContainerSlots属性が追加されます。

あなただけのすべてのPlateContainerモデルにそのカウントクエリを持っているしたい場合は、ミューテータを使用できます。

https://laravel.com/docs/5.3/eloquent-mutators

アイデアは、あなたのモデルに$追加プロパティを設定するために、カスタムを定義することですあなただけがindexアクションでその数をしたい場合は、多分あなただけの代わりにPlateContainerであり、あなたのクエリを返す必要があります

$appends = ['my_count']; 

public function getMyCountAttribute() 
{ 
    return; //return your DB query here 
} 

::のpaginate();:ゲッターの属性

願わくは、これが役に立たない場合は、お気軽にコメントしてください!

+0

私は投稿を更新しました。あなたは一見を持てますか?ありがとう! – user3641381

関連する問題