單獨陣列I對錶size_chart組由尺寸類別如laravel PHP
| id | category | size | created_at | updated_at | deleted_at |
+----+----------+------+---------------------+------------+------------+
| 1 | Regular | S | 2017-10-11 09:35:14 | NULL | NULL |
| 2 | Regular | XS | 2017-10-11 09:39:34 | NULL | NULL |
| 3 | Regular | M | 2017-10-11 09:39:37 | NULL | NULL |
| 4 | Regular | xxl | 2017-10-11 13:03:52 | NULL | NULL |
| 5 | Regular | l | 2017-10-11 13:03:59 | NULL | NULL |
| 6 | Regular | xm | 2017-10-11 13:04:03 | NULL | NULL |
| 7 | Small | em | 2017-10-11 13:05:04 | NULL | NULL |
| 8 | standard | em | 2017-10-13 07:16:50 | NULL | NULL |
在此表中我有一些樣本數據。我需要組基於 類別For.eg
Regular = [s,xs,M,l,xm]
standard = [em]
small = [em]
大小如何在我的PHP控制器功能實現這一
public function sizeFetch(Request $request){
$cat_id = $request->id;
$size_id = SizeCategory::where('category_id',$cat_id)->pluck('size_id');
$size_details = SizeChart::whereIn('id',$size_id)->get();
return $size_details;
}
現在這個函數返回這樣
[
{
id: 1,
category: "Regular",
size: "S",
created_at: "2017-10-11 09:35:14",
updated_at: null,
deleted_at: null
},
{
id: 2,
category: "Regular",
size: "XS",
created_at: "2017-10-11 09:39:34",
updated_at: null,
deleted_at: null
},
{
id: 3,
category: "Regular",
size: "M",
created_at: "2017-10-11 09:39:37",
updated_at: null,
deleted_at: null
},
{
id: 7,
category: "Small",
size: "em",
created_at: "2017-10-11 13:05:04",
updated_at: null,
deleted_at: null
}
]
請幫我解決這個問題
在查詢中使用'groupby'。 – urfusion
選擇尺寸並按類別分組。雖然我很確定如何用雄辯的方式做到這一點。 – hungrykoala
試試這個'SizeChart :: whereIn('id',$ size_id) - > groupBy('category') - > get();'讓我知道它是否工作:) – Maraboc