PHP code example of iamankitthapar / laravel-csv-export

1. Go to this page and download the library: Download iamankitthapar/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/ */

    

iamankitthapar / laravel-csv-export example snippets


use IamAnkitThapar\LaravelCsvExport\Facades\CsvExport;

return CsvExport::download([
    [
        'id' => 1,
        'name' => 'John Doe',
        'email' => '[email protected]',
    ],
], 'users.csv');

use IamAnkitThapar\LaravelCsvExport\Facades\CsvExport;
use App\Models\User;

return CsvExport::download(
    User::query()
        ->select('id', 'name', 'email')
        ->get(),
    'users.csv'
);

return CsvExport::download(
    User::query()
        ->select('id', 'name', 'email')
        ->lazyById(1000),
    'users.csv'
);

return CsvExport::download(
    User::query()
        ->select('id', 'name', 'email')
        ->cursor(),
    'users.csv'
);

return CsvExport::download(
    data: User::query()->lazyById(1000),
    filename: 'users.csv',
    progress: function (int $row, mixed $user): void {
        if ($row % 1000 === 0) {
            logger()->info("Exported {$row} users");
        }
    }
);

return CsvExport::download(
    data: $users,
    filename: 'users.csv',
    headers: [
        'User ID',
        'Full Name',
        'Email Address',
    ]
);

CsvExport::store(
    data: $users,
    path: 'exports/users.csv',
    disk: 'local'
);

$csv = CsvExport::toString($users);
bash
php artisan vendor:publish --tag=csv-export-config