5
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Laravel5でLeague/CSVを使ってCSV作成&ディスクに保存をサクッと実装する

Last updated at Posted at 2015-06-03

League/CSVをダウンロードします。

composer require league/csv

そうすると、composer.jsonにこんなのが追記されます。

"league/csv": "^7.1"

あとは、該当のコントローラーで下記のように記載すればOK

// Writerのinclude
use League\Csv\Writer;

// 空のCSVオブジェクトを作成
$csv = Writer::createFromFileObject(new \SplTempFileObject());

// レコード追加
$csv->insertOne(array('id','name')); // これがheader行
$csv->insertOne(array('1','hoge'));
$csv->insertOne(array('2','hogemi'));

// CSVで保存(時間でファイルを作っています)
Storage::disk('local')->put(time().'.csv',$csv->__toString());

その他の関数等はこちらをご覧ください。
http://csv.thephpleague.com/

5
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?