Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在laravel php中按大小类别分组为单独的数组_Php_Laravel 5 - Fatal编程技术网

在laravel php中按大小类别分组为单独的数组

在laravel php中按大小类别分组为单独的数组,php,laravel-5,Php,Laravel 5,我有一张桌子大小的图表 | id | category | size | created_at | updated_at | deleted_at | +----+----------+------+---------------------+------------+------------+ | 1 | Regular | S | 2017-10-11 09:35:14 | NULL | NULL | | 2 | Regular | X

我有一张桌子大小的图表

| 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       |
在这个表中,我有一些示例数据。我需要根据类别对大小进行分组 例如

如何在我的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能帮你的是什么。
SizeCategory::where('category_id',$cat_id)->groupBy('size')->pull('size_id')


更多信息

如果在得到结果后进行分组,可能会更容易。在查询中进行分组的问题在于,它实际上并没有达到您所期望的效果。(
groupby
在SQL中可能应该改为
AGGREGATE BY


在查询中使用
groupby
。选择大小并按类别进行分组。尽管我太确定如何使用eloquent做到这一点。试试这个
SizeChart::where('id',$size_id)->groupBy('category')->get()并让我知道它是否有效:)
[
{
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
}
]
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()->groupBy("category") //Group by category
             ->map(function ($group) {
                   return $group->pluck("size"); //Pluck size from each category member
             });

    return $size_details;
}