How to chunk results from a custom query in Laravel

前端 未结 8 1352
北海茫月
北海茫月 2020-12-17 18:37

I have a custom query that grabs data from the old system and maps it to models in the new system. The query looks like this:

$companies = DB::connection(\'leg

8条回答
  •  悲&欢浪女
    2020-12-17 19:30

    The chunk feature is only available for Eloquent models and QueryBuilder requests, e.g.

    DB::table('tbl')->where('num', '>', 3)->chunk(500, function($rows) {
        // process $rows
    });
    

    But it won't work for DB::select('...') request. You need to either use a QueryBuilder request, or use an underlying PDO object to query the database, e.g:

    $pdo = DB::getPdo();
    $sth = $pdo->prepare("SELECT ....");
    $sth->execute();
    while ($row = $sth->fetch(PDO::FETCH_ASSOC))
    {
        // ...
    }
    

提交回复
热议问题