PHP code example of bayareawebpro / laravel-simple-csv

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

    

bayareawebpro / laravel-simple-csv example snippets


use BayAreaWebPro\SimpleCsv\SimpleCsv;
use BayAreaWebPro\SimpleCsv\Casts\EmptyValuesToNull;
use BayAreaWebPro\SimpleCsv\Casts\NumericValues;

$lazyCsvCollection = SimpleCsv::import(storage_path('collection.csv'), [
    EmptyValuesToNull::class,
    NumericValues::class,
]);

 declare(strict_types=1);

namespace App\Csv\Casts;

use Carbon\Carbon;

class Timestamps
{
    /** Invoked for each row in import collection. */
    public function __invoke(array $item): array
    {
        foreach ($item as $key => $value){
            if(in_array($key, ['created_at', 'updated_at'])){
                $item[$key] = Carbon::parse($value);
            }
        }
        return $item;
    }
}

use BayAreaWebPro\SimpleCsv\SimpleCsv;

// Collection
SimpleCsv::export(
    Collection::make(...),
    storage_path('collection.csv')
);

// LazyCollection
SimpleCsv::export(
    LazyCollection::make(...),
    storage_path('collection.csv')
);

// Generator (Cursor)
SimpleCsv::export(
    User::query()->where(...)->limit(500)->cursor(),
    storage_path('collection.csv')
);

// Array
SimpleCsv::export(
    [...],
    storage_path('collection.csv')
);

use BayAreaWebPro\SimpleCsv\SimpleCsv;

return SimpleCsv::download([...], 'download.csv');

use Illuminate\Support\Facades\Config;

Config::set('simple-csv.delimiter', ...);
Config::set('simple-csv.enclosure', ...);
Config::set('simple-csv.escape', ...);

return [
    'delimiter' => '?',
    'enclosure' => '?',
    'escape'    => '?',
];