Try this
App\Resource::cursor()->map(function($item) {
dump($item);
});
Hey there! I'm having troubles getting the new Lazy Collections to work.
I'm not working in Laravel, but using the Laravel Container and Database components as composer dependencies directly.
"illuminate/container": "^6.6.1",
"illuminate/database": "^6.6.1",
Everything is working fine so far, I'm using Eloquent Models and the Query Builder as in every other Laravel Application. No problem there.
The problem is there's a huge postgres database with 100.000+ records per table. At one point i want to fetch all instances form a table (the table is called resources) and iterate over each instance, doing some checks, updating, etc... Because of the huge memory load (actually the php process gets terminated after a while) when just doing App\Resource::all()
I want to use Lazy Collections for this task, because as I (thought) I understood it this will resolve that problem altogether.. But something isn't working as I would expect. When doing
$resources = App\Resource::cursor();
dd($resources);
I'll get a Illuminate\Support\LazyCollection back. So far so good.
But when I try to iterate over the results (or even just dump the first result) everything crashes like before, when just doing the App\Resource::all() thing.
What am I doing wrong? Or do I have a misunderstanding on how to use Lazy Collections or what they are for?
Again, to clearify, all these examples crash (php and db process running like wild and get terminated after some time), they're not working for me:
$resources = App\Resource::cursor();
foreach ($resources as $resource) {
// do something
echo $resource->name;
// crashes
}
$resources = App\Resource::cursor();
dd($resources->first()); // also crashes
Would appreciate any help or hints on how to further debug this behaviour..
Thanks a lot in advance!
Please or to participate in this conversation.