Download the PHP package vitorccs/laravel-csv without Composer
On this page you can find all versions of the php package vitorccs/laravel-csv. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package laravel-csv
Laravel CSV
PHP Laravel package to export and import CSV files in a memory-optimized way.
Description
Export CSV files from PHP arrays, Laravel Collections or Laravel Queries and choose to prompt the user to download the file, store it in a Laravel disk or create the file in background as a Laravel Job.
Import CSV files from Laravel Disks, local files, strings or resources and choose to retrieve the full content or in small chunks.
The memory usage is optimized in this project by using PHP streams, which places the content in a temporary file (rather than PHP thread memory) and reads/writes content one line at a time.
NOTE: This project was inspired on https://github.com/maatwebsite/Laravel-Excel which is a great project and can handle many formats (Excel, PDF, OpenOffice and CSV). But since it uses PhpSpreadsheet, it is not optimized for handling large CSV files (thousands of records) causing the PHP memory exhaustion.
Upgrading from v1.0 to v2.0
Version 2.0 adds the importing feature so the only required action is to change the importing namespace:
Requirements
- PHP >= 8.0
- Laravel >= 6.x
Installation
Step 1) Add composer dependency
Step 2) Publish the config file
Step 3) Edit your local config\csv.php
file per your project preferences
How to Export
Step 1) Create an Export class file as shown below
Note: you may implement FromArray, FromCollection or FromQuery
Step 2) The file can now be generated by using a single line:
In case you want the file to be stored in the disk:
You may also get the content as stream for better control over the output:
For larger files, you may want to generate the file in background as a Laravel Job
Export - Data sources
Note: Only FromQuery
can chunk results per chunk_size
parameter from config file.
Laravel Eloquent Query Builder
Laravel Database Query Builder
Laravel Collection
Laravel LazyCollection
PHP Arrays
How to Import
Step 1) Create an Import class file as shown below
Note: you may implement FromDisk, FromFile, FromResource or FromContents
Step 2) The content can now be retrieved by using a single line:
Import - Data sources
From string
From local File
From resource
From Laravel Disk
Implementations
The implementations below work with both Export and Import mode.
Headings
Implement WithHeadings
for setting a heading to the CSV file.
Mapping rows
Implement WithMapping
if you either need to set the value of each column or apply some custom formatting.
Formatting columns
Implement WithColumnFormatting
to format date and numeric fields.
In export mode, the Date must be either a Carbon or a Datetime object, and the number must be any kind of numeric data (numeric string, integer or float).
In import mode, the string content must match with the formatting set (e.g: yyyy-mm-dd for dates).
The formatting preferences are set in the config file csv.php
.
Limiting the results
Implement the method below if you need to limit the quantity of results to be exported/imported.
License
Released under the MIT License.