RoboRobok's avatar

Fetching database row by row in Eloquent

Is it even possible to fetch database in memory-efficient way with Eloquent, row by row? I remember looking for this at some point a while ago and not finding any way to do that.

0 likes
9 replies
Snapey's avatar

It wouldn't be very efficient in other ways, for instance having a new DB query for every row

You are considering this as a solution for some other problem? Might be better to explain the root issue.

RoboRobok's avatar

@Snapey I'm not talking about having a new query for each row. I'm talking about fetching row-by-row, like with old mysql_fetch_XXX functions. So the engine would return one record at a time and move the cursor instead of returning all results at once.

PHPLaraDev's avatar

@Snapey i have problem with swiperjs can you go to my forum and try to help me : D, thanks.

MohamedTammam's avatar

You can use chunk

For example:

Post::chunk(100, function($posts) {
	$posts->each(function($post) {
		// Your logic.
	});
});

You can change the 100 to be 1, but I don't see the benefit for fetching 1 record at a time.

Please or to participate in this conversation.