Framework: get chunk loop count

Created on 17 Jun 2017  路  2Comments  路  Source: laravel/framework

hello .

i want set variable to chunk loop and get this variable but set value of $n don't work .

    $n = 1;
    $tags = \DB::table('posts')
        ->leftJoin('post_tag', 'posts.id', '=', 'post_tag.post_id')
        ->leftJoin('tags', 'tags.id', '=', 'post_tag.tag_id')
        ->orderBy('posts.updated_at' , 'DESC')
        ->select('posts.created_at as ptc','posts.id', 'posts.updated_at as ptu', 'posts.title','tags.created_at','tags.updated_at as tgc','tags.title')
        ->groupBy('tags.id')
        ->chunk(500, function ($tags) use($sitemap , $n) {
            $lastUpdate = $tags->max('ptu');
            $listSiteMapBlogs[] = $lastUpdate;

            $sitemap->addSitemap(\URL::to('tags-sitemap.xml?page='.$n) , $lastUpdate);
            $n++;
        });

Most helpful comment

@m-alinasab, $n isn't being incremented because PHP is passing $n by value, which is the default behavior for everything in PHP with the exception of objects and resources, I believe. If you want this to work, you just need to change your use statement to use($sitemap, &$n), which will pass $n by reference.

All 2 comments

@m-alinasab, $n isn't being incremented because PHP is passing $n by value, which is the default behavior for everything in PHP with the exception of objects and resources, I believe. If you want this to work, you just need to change your use statement to use($sitemap, &$n), which will pass $n by reference.

Please ask questions on the forums, this repo is for bug reporting only.

Was this page helpful?
0 / 5 - 0 ratings