PHP code example of kfoobar / laravel-csv-response

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

    

kfoobar / laravel-csv-response example snippets


return response()->csv(
    rows: [], 
    options: [],
    inline: true // false = force download
);

$rows = Product::all()
    ->map(function (Product $product) {
        return [
            $product->sku,
            $product->name,
            $product->price,
        ];
    })
    ->toArray();

return response()->csv($rows, inline: true);

$rows = Product::all()
    ->map(function (Product $product) {
        return [
            $product->sku,
            $product->name,
            $product->price,
        ];
    })
    ->toArray();

return response()->csv($rows, inline: false);

$rows = Product::all()
    ->map(function (Product $product) {
        return [
            $product->sku,
            $product->name,
            $product->price,
        ];
    })
    ->toArray();

$options = [
    'delimiter' => ',',
    'filename'  => 'catalog.csv',
    'headers'   => ['sku', 'name', 'price'],
];

return response()->csv($rows, $options, inline: false);
bash
php artisan vendor:publish --tag=csv-response-config