dqkx69935 2016-03-22 05:51 采纳率: 100%
浏览 46
已采纳

如何从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;
  • 写回答

1条回答 默认 最新

  • douban2014 2016-03-22 06:10
    关注

    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;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀