<small id='lvWCd'></small><noframes id='lvWCd'>

      <bdo id='lvWCd'></bdo><ul id='lvWCd'></ul>
    <legend id='lvWCd'><style id='lvWCd'><dir id='lvWCd'><q id='lvWCd'></q></dir></style></legend>

    <i id='lvWCd'><tr id='lvWCd'><dt id='lvWCd'><q id='lvWCd'><span id='lvWCd'><b id='lvWCd'><form id='lvWCd'><ins id='lvWCd'></ins><ul id='lvWCd'></ul><sub id='lvWCd'></sub></form><legend id='lvWCd'></legend><bdo id='lvWCd'><pre id='lvWCd'><center id='lvWCd'></center></pre></bdo></b><th id='lvWCd'></th></span></q></dt></tr></i><div id='lvWCd'><tfoot id='lvWCd'></tfoot><dl id='lvWCd'><fieldset id='lvWCd'></fieldset></dl></div>
        <tfoot id='lvWCd'></tfoot>

        如何在 Laravel 中对自定义查询的结果进行分块

        时间:2023-09-23
      1. <tfoot id='PGUUx'></tfoot>
        • <small id='PGUUx'></small><noframes id='PGUUx'>

        • <i id='PGUUx'><tr id='PGUUx'><dt id='PGUUx'><q id='PGUUx'><span id='PGUUx'><b id='PGUUx'><form id='PGUUx'><ins id='PGUUx'></ins><ul id='PGUUx'></ul><sub id='PGUUx'></sub></form><legend id='PGUUx'></legend><bdo id='PGUUx'><pre id='PGUUx'><center id='PGUUx'></center></pre></bdo></b><th id='PGUUx'></th></span></q></dt></tr></i><div id='PGUUx'><tfoot id='PGUUx'></tfoot><dl id='PGUUx'><fieldset id='PGUUx'></fieldset></dl></div>

        • <legend id='PGUUx'><style id='PGUUx'><dir id='PGUUx'><q id='PGUUx'></q></dir></style></legend>

            <bdo id='PGUUx'></bdo><ul id='PGUUx'></ul>
              <tbody id='PGUUx'></tbody>
                  本文介绍了如何在 Laravel 中对自定义查询的结果进行分块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有一个自定义查询,它从旧系统中获取数据并将其映射到新系统中的模型.查询如下所示:

                  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('legacy')->select("...");

                  由于数据量很大,我想使用 Eloquent 的块功能(只是从他们的文档中复制的示例代码):

                  And since it's a lot of data, I'd like to use Eloquent's chunk feature (just sample code copied from their docs):

                  User::chunk(200, function($users)
                  {
                      foreach ($users as $user)
                      {
                          //
                      }
                  });
                  

                  我该如何实施?

                  我的代码现在看起来像这样,导致没有响应:

                  My code now looks like this, which results in no response:

                  DB::connection('legacy')->select("SELECT * FROM companies")->chunk(200, function($companies) {
                      foreach ($companies as $company) {
                          // dd($company);
                          $entity       = Entity::firstOrNew(['external_id' => $company->companyKey]);
                          $entity->name = $company->companyName;
                          $entity->save();
                      }
                  });
                  

                  推荐答案

                  试试这个:

                  <?php
                  
                  $max = 100;
                  $total = DB::connection('legacy')->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. */
                  }
                  

                  基本上复制了 Laravel 的分页器所做的事情,而没有额外的开销.

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

                  这篇关于如何在 Laravel 中对自定义查询的结果进行分块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Laravel Eloquent Query Builder 默认的 Where 条件 下一篇:在laravel eloquent中从数据透视表中获取计数

                  相关文章

                  最新文章

                  1. <tfoot id='ld7SP'></tfoot>
                      <bdo id='ld7SP'></bdo><ul id='ld7SP'></ul>

                    1. <small id='ld7SP'></small><noframes id='ld7SP'>

                      <i id='ld7SP'><tr id='ld7SP'><dt id='ld7SP'><q id='ld7SP'><span id='ld7SP'><b id='ld7SP'><form id='ld7SP'><ins id='ld7SP'></ins><ul id='ld7SP'></ul><sub id='ld7SP'></sub></form><legend id='ld7SP'></legend><bdo id='ld7SP'><pre id='ld7SP'><center id='ld7SP'></center></pre></bdo></b><th id='ld7SP'></th></span></q></dt></tr></i><div id='ld7SP'><tfoot id='ld7SP'></tfoot><dl id='ld7SP'><fieldset id='ld7SP'></fieldset></dl></div>
                      <legend id='ld7SP'><style id='ld7SP'><dir id='ld7SP'><q id='ld7SP'></q></dir></style></legend>