php – Laravel chunk()与array_chunk错误?

我正在与Laravel 5合作开展一个本地项目.我正试图抓住可能成为(或变成)大量数据的东西.我这样做了

    $tickets = Ticket::getResolvedTicketsBetween($start,$end)->chunk(200, function($chunkOfTickets){
    foreach ($chunkOfTickets as $ticket) {
        echo $ticket->id;
    }
});

问题是我收到了错误

array_chunk() expects parameter 3 to be boolean, object given

我究竟做错了什么?有人可以帮助我,我正在遵循文件……我想…

最佳答案 我刚刚搜索了Laravel框架,唯一的用法是在Collection类中,它有一个chunk()函数,不要与查询构建器类的chunk()函数混淆.

> http://laravel.com/api/5.0/Illuminate/Support/Collection.html
> http://laravel.com/api/5.0/Illuminate/Database/Query/Builder.html

如果getResolvedTicketsBetween()调用get(),那么它将最终返回一个Collection.如果您希望能够继续构建查询,请删除对get()的调用.

我猜你的代码看起来如何:

function getResolvedTicketsBetween($start, $end) {

    // Dont do this
    // return Ticket::where('created_at', '>=', $start)->where('created_at', '<=', $end)->where('state','=','Resolved')->get()

    // Do this instead (returns Query Builder instance)
    return Ticket::where('created_at', '>=', $start)->where('created_at', '<=', $end)->where('state','=','Resolved');
}
点赞