1. Go to this page and download the library: Download elrayes/laravel-csv-export 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/ */
elrayes / laravel-csv-export example snippets
namespace App\Export;
use App\Models\User;
use Elrayes\LaravelCsvExport\Exporters\BaseExporter;
use Illuminate\Contracts\Database\Eloquent\Builder as BuilderContract;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Collection;
class UserCSVExporter extends BaseExporter
{
protected bool ${
return [
$row->id,
$row->name,
$row->email,
optional($row->created_at)->toDateTimeString(),
];
}
}
use Elrayes\LaravelCsvExport\Facades\CSVExport;
use App\Exporters\UserCSVExporter;
// Store on S3 at exports/users.csv
$stored = CSVExport::store(UserCSVExporter::class, 'exports/users.csv', 's3');
// returns 'exports/users.csv' if successful
$exporter = app(UserCSVExporter::class)
->setChunkSize(2000)
->setMaxLimit(50000)
->setUseMaxLimit(true)
->setIncludeBom(true);
// When resolving via the Facade, you can bind a configured instance in the container
app()->bind(UserCSVExporter::class, fn () => $exporter);
CSVExport::stream(UserCSVExporter::class, 'users.csv');
use Elrayes\LaravelCsvExport\Exporters\BaseExporter;
use App\Models\User;
use Illuminate\Contracts\Database\Eloquent\Builder as BuilderContract;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Collection;
/**
* @extends BaseExporter<User>
*/
class UserCSVExporter extends BaseExporter
{
public function query(): Builder|Collection|BuilderContract
{
return User::query()->select(['id', 'name', 'email', 'created_at']);
}
public function headings(): array
{
return ['ID', 'Name', 'Email', 'Registered At'];
}
/** @param User $row */
public function map(mixed $row): array
{
return [
$row->id,
$row->name,
$row->email,
optional($row->created_at)->toDateTimeString(),
];
}
}
bash
php artisan make:export UserCSVExporter
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.