Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/62.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.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
Mysql 在Eloquent中使用GroupBy和分页时出错_Mysql_Laravel_Pagination_Eloquent_Laravel Eloquent - Fatal编程技术网

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

Mysql 在Eloquent中使用GroupBy和分页时出错,mysql,laravel,pagination,eloquent,laravel-eloquent,Mysql,Laravel,Pagination,Eloquent,Laravel Eloquent,我试图使用eloquent让我得到一个分组响应,同时给我一个分页响应(给我第二页链接的那个)。 我正在尝试这样做: 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

我试图使用eloquent让我得到一个分组响应,同时给我一个分页响应(给我第二页链接的那个)。 我正在尝试这样做:

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);
但是,在修补程序中运行时,我遇到了以下错误:

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():

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

谢谢,
João

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

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);
另外,只是一个提示,你不需要用碳来解析日期,雄辩为你做到这一点


或者,您可以尝试使用
illighte\Pagination\LengthAwarePaginator
对集合进行分组

$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();

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

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);
另外,只是一个提示,你不需要用碳来解析日期,雄辩为你做到这一点


或者,您可以尝试使用
illighte\Pagination\LengthAwarePaginator
对集合进行分组

$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();

谢谢你的回复!问题是我丢失了分页的结果。我确实需要下一个页面链接,但使用这种方法,我将数据分组,但没有分页。结果:=>Illumb\Database\Eloquent\Collection{#891 all:[“2017-07-05”=>Illumb\Database\Eloquent\Collection{#909 all:[App\Eating{945 id:5,状态:“Comeu Bem”,创建于:“2017-07-05 13:55:25”,},,},啊,我现在明白了,它是如何不分页的…在阅读了文档之后,尽管看起来你不能同时使用这两个:目前,Laravel无法有效执行使用groupBy语句的分页操作。如果需要使用带有分页结果集的groupBy,建议您查询数据库并手动创建分页器。“谢谢你的回复!我的问题是我丢失了分页结果。我确实需要下一个页面链接,但使用这种方法,我将数据分组,但没有分页。结果:=>Illumb\Database\Eloquent\Collection{891 all:[“2017-07-05”=>Illumb\Database\Eloquent\Collection{909 all:[App\Eating{#945id:5,状态:“Comeu Bem”,创建于:“2017-07-05 13:55:25”,},},},},},},啊,我现在看到它是如何没有分页的…在阅读文档后,尽管看起来你不能同时使用这两个:目前,Laravel无法有效地执行使用groupBy语句的分页操作。如果需要使用带有分页结果集的groupBy,建议您查询数据库并手动创建分页器