小编Fer*_*nir的帖子

Laravel 中的 Groupby 将分组项保留在对象中

是的,不确定是我做错了什么还是 Laravel 中 Illuminate\Database 的问题。

我的代码:

$sth = Insect::leftJoin('types', 'types.id', '=', 'families.type_id')
            ->select('types.name as types','families.id','families.name')
            ->get()
            ->groupBy('types');
Run Code Online (Sandbox Code Playgroud)

groupBy之前的结果是:

[
{
    "types": "moths",
    "id": 1,
    "name": "Bombycidae"
},
{
    "types": "moths",
    "id": 2,
    "name": "Brahmaeidae"
},
{
    "types": "moths",
    "id": 3,
    "name": "Cossidae"
},
{
    "types": "larvas",
    "id": 6,
    "name": "test"
}]
Run Code Online (Sandbox Code Playgroud)

但对于 groupBy:

{
"moths": [
    {
        "types": "moths",
        "id": 1,
        "name": "Bombycidae"
    },
    {
        "types": "moths",
        "id": 2,
        "name": "Brahmaeidae"
    },
    {
        "types": "moths",
        "id": 3,
        "name": "Cossidae"
    } …
Run Code Online (Sandbox Code Playgroud)

php group-by laravel

2
推荐指数
1
解决办法
7653
查看次数

标签 统计

group-by ×1

laravel ×1

php ×1