How to chunk results from a custom query in Laravel

前端 未结 8 1344
北海茫月
北海茫月 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:35

    Try something like this:

    select("...")->count();
    $pages = ceil($total / $max);
    for ($i = 1; $i < ($pages + 1); $i++) {
        $offset = (($i - 1)  * $max);
        $start = ($offset == 0 ? 0 : ($offset + 1));
        $legacy = DB::connection('legacy')->select("...")->skip($start)->take($max)->get();
        /* Do stuff. */
    }
    

    Basically duplicates what Laravel's Paginator does without the extra overhead.

提交回复
热议问题