Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.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
Php GroupBy的larvel用法_Php_Laravel_Laravel 4 - Fatal编程技术网

Php GroupBy的larvel用法

Php GroupBy的larvel用法,php,laravel,laravel-4,Php,Laravel,Laravel 4,我正试图通过cat_id对表中的数据进行分组,并将结果转换为JSON 表结构 id name cat_id 1 A 2 2 B 1 3 C 2 4 D 2 5 E 3 6 F 2 7 G 1 这是我试过的代码 $posts = Posts::groupBy('cat_id')->get(); return $p

我正试图通过
cat_id
对表中的数据进行分组,并将结果转换为JSON

表结构

id  name  cat_id   
1    A      2 
2    B      1    
3    C      2     
4    D      2    
5    E      3     
6    F      2      
7    G      1   
这是我试过的代码

$posts = Posts::groupBy('cat_id')->get();

return $posts->toJson();
结果应该如下所示:

[
    {
        "id": "1", //cat_id
        "posts": [
            {
                "id": "2", //user id
                "s": "B"  //name
            },
            {
                "id": "7",
                "s": "G"
            }
        ]
    },
    {
        "id": "2", //cat id
        "posts": [
            {
                "id": "1",
                "name": "A"
            },
            {
                "id": "3",
                "name": "C"
            },
            {
                "id": "4",
                "s": "D"
            },
            {
                "id": "6",
                "s": "F"
            }
        ]
    }
]
class Category extends Eloquent {
    public function posts(){
        return $this->hasMany('Post', 'cat_id');
    }
}
{
  "1":  // cat_id
    [
      {
         "id": "5"  // post data
         ...
      },
      ...
    ],
  "2":
    ...
}

但是我得到了对非对象上的成员函数toJson()的
调用。我哪里做错了?

不幸的是,
groupBy
不能这样工作。它更像是SQLs
groupby
(实际上它在后台就是这样做的…)

我假设您的错误来自这样一个事实:它不知道如何处理
name
id
,因为您总是需要使用
groupby
聚合字段

下面是你如何实现目标的方法。我假设您已经设置了
类别
发布
之间的关系。有点像这样:

[
    {
        "id": "1", //cat_id
        "posts": [
            {
                "id": "2", //user id
                "s": "B"  //name
            },
            {
                "id": "7",
                "s": "G"
            }
        ]
    },
    {
        "id": "2", //cat id
        "posts": [
            {
                "id": "1",
                "name": "A"
            },
            {
                "id": "3",
                "name": "C"
            },
            {
                "id": "4",
                "s": "D"
            },
            {
                "id": "6",
                "s": "F"
            }
        ]
    }
]
class Category extends Eloquent {
    public function posts(){
        return $this->hasMany('Post', 'cat_id');
    }
}
{
  "1":  // cat_id
    [
      {
         "id": "5"  // post data
         ...
      },
      ...
    ],
  "2":
    ...
}
然后您可以执行以下操作:

$categories = Category::with('posts')->get();

您可以获得包含其
帖子的所有类别
toJson()
也应该可以正常工作。

@lukasgeiter建议做正确的事情,不过还有另一种方法。根据您的需要,您可以为自己选择:

$posts = Pots::all(); // fetch from the DB

$groupedByCat = $posts->groupBy('cat_id'); // call group by on the collection
结果如下所示:

[
    {
        "id": "1", //cat_id
        "posts": [
            {
                "id": "2", //user id
                "s": "B"  //name
            },
            {
                "id": "7",
                "s": "G"
            }
        ]
    },
    {
        "id": "2", //cat id
        "posts": [
            {
                "id": "1",
                "name": "A"
            },
            {
                "id": "3",
                "name": "C"
            },
            {
                "id": "4",
                "s": "D"
            },
            {
                "id": "6",
                "s": "F"
            }
        ]
    }
]
class Category extends Eloquent {
    public function posts(){
        return $this->hasMany('Post', 'cat_id');
    }
}
{
  "1":  // cat_id
    [
      {
         "id": "5"  // post data
         ...
      },
      ...
    ],
  "2":
    ...
}

没有toJson你会得到结果吗?@AnandPatel我会得到结果,但不是我预期的结果。这个查询显然有问题。