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;
1
  • 1
    I got the answer. It resolved with "function ($messages) use (&$p_ids)". Commented Mar 22, 2016 at 6:02

1 Answer 1

8

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;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.