How to chunk results from a custom query in Laravel

0 votes

I have a custom query that grabs data from the old system and maps it to models in the new system. The query looks like this:

$companies = DB::connection('legacy')->select("...");

And since it's a lot of data, I'd like to use Eloquent's chunk feature (just sample code copied from their docs):

User::chunk(200, function($users)
{
    foreach ($users as $user)
    {
        //
    }
});

How do I implement this?

Dec 7, 2020 in Laravel by kartik
• 37,520 points
2,279 views

1 answer to this question.

0 votes

Hello @kartik,

Try something like this:

<?php

$max = 100;
$total = DB::connection('legacy')->select("...")->count();
$pages = ceil($total / $max);
for ($i = 1; $i < ($pages + 1); $i++) {
    $offset = (($i - 1)  * $max);
    $start = ($offset == 0 ? 0 : ($offset + 1));
    $legacy = DB::connection('legacy')->select("...")->skip($start)->take($max)->get();
    /* Do stuff. */
}

Hope it helps!!

Thank you!!

answered Dec 7, 2020 by Niroj
• 82,840 points

Related Questions In Laravel

0 votes
1 answer

How to select all column name from a table in laravel?

You can get all columns name by ...READ MORE

answered Dec 2, 2020 in Laravel by Niroj
• 82,840 points
6,452 views
0 votes
1 answer

How to mock a static facade methods in Laravel?

Hey, Facades provide a "static" interface to classes ...READ MORE

answered Mar 19, 2020 in Laravel by Niroj
• 82,840 points
6,538 views
0 votes
1 answer

How to enable query log in Laravel?

Hello, You can use simple function that is ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,840 points
2,525 views
0 votes
1 answer

.How to turn off CSRF protection for a particular route in Laravel?

Hey, We can add that particular URL or ...READ MORE

answered Mar 24, 2020 in Laravel by Niroj
• 82,840 points
4,213 views
+1 vote
1 answer

How to make anchor tag with routing using Laravel?

Hey @kartik, First you have to go to ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,840 points
23,012 views
0 votes
1 answer

What is redirection in Laravel?

Named route is used to give specific ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,840 points
2,994 views
0 votes
1 answer

How to install Laravel via composer?

Hello, This is simple you just need to ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,840 points
2,918 views
+1 vote
1 answer

What are named routes in Laravel and How can specify route names for controller actions?

Hey @kartik, Named routing is another amazing feature of ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,840 points
44,691 views
0 votes
1 answer

How to gett selected values from a multiple select form in Laravel?

Hello @kartik, First, if you want to have ...READ MORE

answered Oct 21, 2020 in Laravel by Niroj
• 82,840 points
7,380 views
0 votes
1 answer

How to alias a table in Laravel Eloquent queries using Query Builder?

Hello @kartik, You can use less code, writing ...READ MORE

answered Nov 11, 2020 in Laravel by Niroj
• 82,840 points
10,548 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP