1. Go to this page and download the library: Download vitorccs/laravel-csv library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
vitorccs / laravel-csv example snippets
// v1.0 (old)
use Vitorccs\LaravelCsv\Concerns\Exportable;
use Vitorccs\LaravelCsv\Concerns\FromArray;
use Vitorccs\LaravelCsv\Concerns\FromCollection;
use Vitorccs\LaravelCsv\Concerns\FromQuery;
// v2.0 (new)
use Vitorccs\LaravelCsv\Concerns\Exportables\Exportable;
use Vitorccs\LaravelCsv\Concerns\Exportables\FromArray;
use Vitorccs\LaravelCsv\Concerns\Exportables\FromCollection;
use Vitorccs\LaravelCsv\Concerns\Exportables\FromQuery;
namespace App\Exports;
use App\User;
use Vitorccs\LaravelCsv\Concerns\Exportables\Exportable;
use Vitorccs\LaravelCsv\Concerns\Exportables\FromQuery;
class UsersExport implements FromQuery
{
use Exportable;
public function query()
{
return User::query()
->where('created_at', '>=', '2024-01-01 00:00:00');
}
}
# prompt the client browser to download the file
return (new UsersExport)->download('users.csv');
# will save the file in 's3' disk
return (new UsersExport)->store('users.csv', 's3');
# will get the content in a stream (content placed in a temporary file)
return (new UsersExport)->stream();
use App\Jobs\NotifyCsvCreated;
# generate a {uuid-v4}.csv filename
$filename = CsvHelper::filename();
# will create a job to create and store the file in disk
# and afterwards notify the user
(new BillsExport())
->queue($filename, 's3')
->allOnQueue('default')
->chain([
// You must create the Laravel Job below
new NotifyCsvCreated($filename)
]);
namespace App\Exports;
use App\User;
use Vitorccs\LaravelCsv\Concerns\Exportables\Exportable;
use Vitorccs\LaravelCsv\Concerns\Exportables\FromQuery;
class MyQueryExport implements FromQuery
{
use Exportable;
public function query()
{
return User::query();
}
}
namespace App\Exports;
use App\User;
use Illuminate\Support\Facades\DB;
use Vitorccs\LaravelCsv\Concerns\Exportables\Exportable;
use Vitorccs\LaravelCsv\Concerns\Exportables\FromQuery;
class MyQueryExport implements FromQuery
{
use Exportable;
public function query()
{
return DB::table('users');
}
}
namespace App\Exports;
use Vitorccs\LaravelCsv\Concerns\Exportables\Exportable;
use Vitorccs\LaravelCsv\Concerns\Exportables\FromCollection;
class MyCollectionExport implements FromCollection
{
use Exportable;
public function collection()
{
return collect([
['a1', 'b1', 'c1'],
['a2', 'b2', 'c2'],
['a3', 'b3', 'c3']
]);
}
}
namespace App\Exports;
use App\User;
use Vitorccs\LaravelCsv\Concerns\Exportables\Exportable;
use Vitorccs\LaravelCsv\Concerns\Exportables\FromCollection;
class MyQueryExport implements FromCollection
{
use Exportable;
public function collection()
{
return User::cursor();
}
}
namespace App\Exports;
use Vitorccs\LaravelCsv\Concerns\Exportables\Exportable;
use Vitorccs\LaravelCsv\Concerns\Exportables\FromArray;
class MyArrayExport implements FromArray
{
use Exportable;
public function array(): array
{
return [
['a1', 'b1', 'c1'],
['a2', 'b2', 'c2'],
['a3', 'b3', 'c3']
];
}
}
namespace App\Exports;
use Vitorccs\LaravelCsv\Concerns\Importables\Importable;
use Vitorccs\LaravelCsv\Concerns\Importables\FromDisk;
class UsersImport implements FromDisk
{
use Importable;
public function disk(): ?string
{
return 's3';
}
public function filename(): string
{
return 'users.csv';
}
}
# get the records in array format
return (new UsersImport)->getArray();
# in case the result is too large, you may receive small chunk of results
# at a time in your callback function, preventing memory exhaustion.
(new UsersImport)->chunkArray(function(array $rows, int $index) {
// do something with the rows
echo "Chunk $index has the following records:";
print_r($rows);
});
namespace App\Imports;
use Vitorccs\LaravelCsv\Concerns\Importables\Importable;
use Vitorccs\LaravelCsv\Concerns\Importables\FromContents;
class MyContents implements FromContents
{
use Importable;
public function contents(): string
{
return "A1,B1,C1\nA2,B2,C2\n,A3,B3,C3";
}
}
namespace App\Imports;
use Vitorccs\LaravelCsv\Concerns\Importables\Importable;
use Vitorccs\LaravelCsv\Concerns\Importables\FromFile;
class MyFileImport implements FromFile
{
use Importable;
public function filename(): string;
{
return storage_path() . '/users.csv';
}
}
namespace App\Imports;
use Vitorccs\LaravelCsv\Concerns\Importables\Importable;
use Vitorccs\LaravelCsv\Concerns\Importables\FromResource;
class MyResourceImport implements FromResource
{
use Importable;
public function resource()
{
$contents = "A1,B1,C1\nA2,B2,C2\n,A3,B3,C3";
$resource = fopen('php://memory', 'w+');
fputs($resource, $contents);
return $resource;
}
}
namespace App\Exports;
use Vitorccs\LaravelCsv\Concerns\Importables\Importable;
use Vitorccs\LaravelCsv\Concerns\Importables\FromDisk;
class UsersImport implements FromDisk
{
use Importable;
public function disk(): ?string
{
return 'local';
}
public function filename(): string
{
return 'my_imports/users.csv';
}
}
use Vitorccs\LaravelCsv\Concerns\Exportables\Exportable;
use Vitorccs\LaravelCsv\Concerns\Exportables\FromArray;
use Vitorccs\LaravelCsv\Concerns\WithHeadings;
class UsersExport implements FromArray, WithHeadings
{
use Exportable;
public function headings(): array
{
return ['ID', 'Name', 'Email'];
}
}
use Vitorccs\LaravelCsv\Concerns\Exportables\Exportable;
use Vitorccs\LaravelCsv\Concerns\Exportables\FromArray;
use Vitorccs\LaravelCsv\Concerns\WithMapping;
class UsersExport implements FromArray, WithMapping
{
use Exportable;
public function map($user): array
{
return [
$user->id,
$user->name,
$user->email ?: 'N/A'
];
}
}
use Carbon\Carbon;
use Vitorccs\LaravelCsv\Concerns\Exportables\Exportable;
use Vitorccs\LaravelCsv\Concerns\Exportables\FromArray;
use Vitorccs\LaravelCsv\Concerns\WithColumnFormatting;
use Vitorccs\LaravelCsv\Enum\CellFormat;
class UsersExport implements FromArray, WithColumnFormatting
{
use Exportable;
public function array(): array
{
return [
[ Carbon::now(), Carbon::now(), 2.50, 1.00 ],
[ new DateTime(), new DateTime(), 3, 2.00 ]
];
}
public function columnFormats(): array
{
return [
'A' => CellFormat::DATE,
'B' => CellFormat::DATETIME,
'C' => CellFormat::DECIMAL,
'D' => CellFormat::INTEGER,
];
}
}
use Vitorccs\LaravelCsv\Concerns\Exportables\Exportable;
use Vitorccs\LaravelCsv\Concerns\Exportables\FromQuery;
class UsersExport implements FromQuery
{
use Exportable;
public function limit(): ?int
{
return 5000;
}
}