简体   繁体   中英

Laravel chunk() mistaken with array_chunk?

I'm working on a local project with Laravel 5. I'm trying to grab what could potentially be (or become) huge amounts of data. I'm doing so

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

The problem is I'm getting the error

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

What am I doing wrong? Can someone please help me, I'm following the documentation accordingly... I think...

I just did a search of the Laravel framework and the only usage is in the Collection class, which has a chunk() function not to be confused with the chunk() function of the query builder class.

If getResolvedTicketsBetween() makes a call to get() then it will end up returning a Collection . If you want to be able to continue building on the query, remove the call to get() .

My guess at how your code might look:

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');
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM