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;

$lazyCollection = SimpleCsv::import(storage_path('collection.csv'));

use BayAreaWebPro\SimpleCsv\SimpleCsv;
use App\SimpleCsv\MyCustomCast;

$lazyCollection = SimpleCsv::import(storage_path('collection.csv'), [
    MyCustomCast::class
]);

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

 declare(strict_types=1);

namespace App\Csv\Casts;

use Carbon\Carbon;
use Illuminate\Config\Repository;

class ParseTimestamps
{
    /** 
     * Inject services by adding a constructor. 
     */
    public function __construct(public Repository $config)
    {
        // 
    }
    
    /** 
     * Invoked for each row. 
     */
    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(
    items: Collection::make(...),
    path: storage_path('collection.csv')
);

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

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

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

use BayAreaWebPro\SimpleCsv\SimpleCsv;
use Symfony\Component\HttpFoundation\StreamedResponse;

public function download(): StreamedResponse
{
    return SimpleCsv::download(
        items: [...], 
        fileName: 'download.csv', 
        headers: ['My-Response-Header' => 'some-value']
    );
}