Laravel 5.6 + maatwebsite/excel 3.0
を使用しています。
PHP
1class UsersExport implements FromCollection { 2 3 public function collection() { 4 return \App\Model\User::orderBy('id', 'DESC')->get(); 5 6 } 7} 8
のクラスにて、EXCELダウンロードすると、
Allowed memory size of 134217728 bytes exhausted (tried to allocate 4194304 bytes)
のメモリエラーになります。
chunk等を使用して、メモリ節約して取得実施したいのですが、
どのような記述になりますでしょうか?
気になる質問をクリップする
クリップした質問は、後からいつでもMYページで確認できます。
またクリップした質問に回答があった際、通知やメールを受け取ることができます。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
回答3件
0
ini_set('memory_limit', '512M');
を設定で解決ずみ
投稿2018/05/07 23:01
総合スコア93
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
0
ベストアンサー
他の回答者様の内容にありますが、From Queryの場合は自動でChunkが適用されます。下記の様に書く事で正常に動作しました。ちなみに、chunkの件数は、config/excel.php
で定義されています。
公式サイト
Controller側
php
1<?php 2 3namespace App\Http\Controllers; 4 5use Illuminate\Http\Request; 6use App\Test\TestExcel; 7 8class homeController extends Controller 9{ 10 public function index(Request $request) { 11 return (new TestExcel())->download('test.xlsx'); 12 } 13}
抽出処理
php
1<?php 2namespace App\Test; 3/* 4 * To change this license header, choose License Headers in Project Properties. 5 * To change this template file, choose Tools | Templates 6 * and open the template in the editor. 7 */ 8 9use Maatwebsite\Excel\Concerns\FromQuery; 10use Maatwebsite\Excel\Concerns\Exportable; 11use App\User; 12 13 14/** 15 * Description of TestExcel 16 * 17 */ 18class TestExcel implements FromQuery { 19 use Exportable; 20 21 public function query(){ 22 //get()は不要なので要注意! 23 return User::orderBy('id', 'DESC'); 24 } 25}
(こちらの情報は古いバージョンの為、3.0には適用できません)
若干、古いバージョンのIssueですが下記が参考になるのではないでしょうか?参考サイト
php
1Excel::create('ExcelFile', function($excel) { 2 $excel->sheet('Sheet1', function($sheet) { 3 ExampleModel::chunk(500, function($modelInstance) use($sheet) { 4 5 $modelAsArray = $modelInstance->toArray(); 6 $sheet->appendRow($modelAsArray); 7 }); 8 }); 9})->export('xls');
投稿2018/04/09 06:19
編集2018/04/09 08:33総合スコア3027
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/04/09 07:25
2018/04/09 07:57
2018/04/09 08:41
2018/04/09 08:52
2018/04/09 09:16
2018/04/09 09:23 編集
2018/04/09 10:26
2018/04/10 00:07
2018/04/10 16:56 編集
2018/04/11 04:38
あなたの回答
tips
太字
斜体
打ち消し線
見出し
引用テキストの挿入
コードの挿入
リンクの挿入
リストの挿入
番号リストの挿入
表の挿入
水平線の挿入
プレビュー
質問の解決につながる回答をしましょう。 サンプルコードなど、より具体的な説明があると質問者の理解の助けになります。 また、読む側のことを考えた、分かりやすい文章を心がけましょう。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/04/09 07:02