简体   繁体   中英

How can I get variable from chunk function of query builder in laravel 5.2?

I am trying to process some of the records in 'message' table using chunk method of laravel 5.2 query builder. But I am unable to get processed ids in array outside of the query builder.

I can access it declaring variable as global but is there any other way?

I need this after completion of chunk because if I update record in same loop then chunk will skip records. As chunk works like pagination.

Using global (Working):

global $m_ids;

DB::table("messages")
    ->where('processed','0')   
    ->chunk(100, function ($messages){
                    foreach ($messages as $message) {
                        $GLOBALS['$m_ids'][] = $message->id;
                    }
                });

echo "<pre>"; print_r($GLOBALS['$m_ids']); die;

Change Code:

$m_id = [];
DB::table("messages")
->where('processed','0')   
->chunk(100, function ($messages) use(&$m_id){
                foreach ($messages as $message) {
                    $m_id[] = $message->id;
                }
            });

echo "<pre>"; print_r($m_id); die;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM