如何从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;