4

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...

Thaenor
  • 611
  • 1
  • 9
  • 28
  • what is the `getResolvedTicketsBetween` method returning? A collection? A query? – Kryten Jun 30 '15 at 16:14
  • that function is actually one line. return Ticket::where('created_at', '>=', $start)->where('created_at', '<=', $end)->where('state','=','Resolved')->get(); – Thaenor Jun 30 '15 at 16:17

1 Answers1

2

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');
}
Jeremy Harris
  • 24,318
  • 13
  • 79
  • 133
  • 1
    AHHH! ok, I get it. It was the get(). With that error I don't think I would've ever figured it out on my own. Many thanks. – Thaenor Jun 30 '15 at 16:19
  • 1
    No problem. Whenever I run into issues with Laravel, I dive into the framework code and see what is going on. It helps so much. `vendor/laravel/framework/src` is your friend. – Jeremy Harris Jun 30 '15 at 16:21
  • On a slightly side question. I can call a function inside the for each like $ticket->updateValue(); and then reffer to that ticket inside the module as $this right? – Thaenor Jun 30 '15 at 16:23
  • I wanted to check the framework code, my phpStorm is acting out it won't let me ctrl click the functions and jump to the code... :/ – Thaenor Jun 30 '15 at 16:24
  • 1
    Another helpful tip in debugging is the use of a library that displays **everything** - I like [Kint](https://packagist.org/packages/raveren/kint). Dump an object to your browser with `\Kint::dump($object)` and you can navigate through a ridiculous amount of information about that object. I often use it to see what classes I'm getting back from the framework. – Kryten Jun 30 '15 at 16:24
  • 1
    @Thaenor No, I don't think you can use `$this` to reference the object in the loop. In Javascript that works due to the way scopes are, but in PHP it will be a reference to the class instance your code is inside. If not a class instance or if a static method, then `$this` will not work at all. – Jeremy Harris Jun 30 '15 at 16:26
  • @cillosis I can see that. Debugging it with kint just realized those objects were null. I'll need to pass the ticket as a parameter. – Thaenor Jun 30 '15 at 16:34
  • I'm doing $ticket->updateTicketPoints($ticket); inside the for each. At this point I should be on a ticket by ticket analysis. However referencing the $ticket inside the model and using Kint's dump it's printing out two different objects? How is this happening? – Thaenor Jun 30 '15 at 16:39
  • I'm not sure, never used kint. – Jeremy Harris Jun 30 '15 at 16:40
  • it's not because of kint. For some reason php is handling "a single ticket" as two tickets. But the code seems to be working (it was written to handle only one ticket at a time). maybe it's some Laravel engine optimization or something. – Thaenor Jun 30 '15 at 16:46
  • I think I got it why this is happening. Does Laravel's chunk run in parallel? Maybe I'm multi-processing two array elements at the same time. @cillosis - PS: Sorry for the spam – Thaenor Jun 30 '15 at 17:06
  • 1
    @Thaenor No, its not parallel. It has a while loop and grabs chunks of records and passes each chunk to your callback. If you `dd($ticket);` is it an instance of your **Ticket** model? (it should be). You can reference the code here: https://github.com/laravel/framework/blob/5.1/src/Illuminate/Database/Query/Builder.php#L1486 – Jeremy Harris Jun 30 '15 at 17:10
  • @cillosis dd($ticket); returns the data for a single ticket. however var_dump($ticket); prints the data of two tickets... no idea why. – Thaenor Jun 30 '15 at 17:49