在 Eloquent 中使用 GroupBy 和分页时出错

Getting error while using GroupBy and Pagination in Eloquent

我正在尝试使用 eloquent 让我按响应分组,同时给我一个分页响应(给我 link 到第二页的响应) . 我正在尝试这样做:

App\Eating::Where('student_id', 2)->orderBy('created_at', 'DESC')->groupBy(function ($row) {
     return Carbon\Carbon::parse($row->created_at)->format('Y-m-d');
})->paginate(25);

但是,当 运行 在 Tinker 中出现此错误时:

PHP warning:  strtolower() expects parameter 1 to be string, object given in D:\Folder\vendor\laravel\framework\src\Illuminate\Database\Grammar.php on line 58

没有 groupBy,我得到了正确的结果:

>>> App\Eating::Where('student_id', 2)->orderBy('created_at', 'DESC')->paginate(25)->toArray();
=> [
     "total" => 1,
     "per_page" => 25,
     "current_page" => 1,
     "last_page" => 1,
     "next_page_url" => null,
     "prev_page_url" => null,
     "from" => 1,
     "to" => 3,
     "data" => [
       [
         "id" => 5,
         "status" => "Comeu Bem",
         "created_at" => "2017-07-05 13:55:25",
         "updated_at" => "2017-07-05 13:55:25",
       ],
     ],
   ]

但是,当我删除分页时,我确实得到了错误,但这只是因为我添加了 get():

>>>  App\Eating::Where('student_id', 2)->orderBy('created_at', 'DESC')->groupBy(function ($row) {
...                          return Carbon\Carbon::parse($row->created_at)->format('Y-m-d');
...                      })->get();
PHP warning:  strtolower() expects parameter 1 to be string, object given in D:\Joao\git\F1Softwares\Code\Server\F1Softwares\vendor\laravel\framework\src\Illuminate\Database\Grammar.php on line 58
>>>
>>>  
>>>  App\Eating::Where('student_id', 2)->orderBy('created_at', 'DESC')->groupBy(function ($row) {
...                          return Carbon\Carbon::parse($row->created_at)->format('Y-m-d');
...                      });
=> Illuminate\Database\Eloquent\Builder {#855}

知道我做错了什么吗?我确实需要 orderBy 和分页,以使应用程序更容易显示结果(这是一个 RestFul 调用)。

谢谢, 若昂

您必须对集合调用 groupBy() 方法,但这似乎不适用于 paginate()。您可以尝试在集合上使用 forPage() 方法:

App\Eating::where('student_id', 2)->orderBy('created_at', 'DESC')
    ->get()->groupBy(function ($eating) {
         return $eating->created_at->format('Y-m-d');
    })->forPage(1, 25);

另外,请注意,您不需要使用 Carbon 来解析日期,Eloquent 会为您完成。


或者,您可以在使用 Illuminate\Pagination\LengthAwarePaginator.

对集合进行分组后尝试 manually create your paginator
$eatings = App\Eating::where('student_id', 2)->orderBy('created_at', 'DESC')
               ->get()->groupBy(function ($eating) {
                   return $eating->created_at->format('Y-m-d');
               });
$paginatedEatings = new LengthAwarePaginator($eatings, $eatings->count(), 25);

return $paginatedEatings->toArray();