PHP code example of audunru / export-response

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

    

audunru / export-response example snippets


'api' => [
    'throttle:api',
    \Illuminate\Routing\Middleware\SubstituteBindings::class,
    \audunru\ExportResponse\Middleware\ExportCsv::class,
    \audunru\ExportResponse\Middleware\ExportXlsx::class,
    \audunru\ExportResponse\Middleware\ExportXml::class,
],

Route::apiResource('documents', DocumentController::class)
    ->middleware([
        ExportCsv::class,
        ExportXlsx::class,
        ExportXml::class
    ])
    ->name('documents');

Route::apiResource('documents', DocumentController::class)
    ->middleware([
        ExportCsv::with([
            'key' => 'data',
        ]),
        ExportXlsx::with([
            'key' => 'data',
        ]),
        ExportXml::class::with([
            'key' => 'data',
        ]),
    ])
    ->name('documents');

protected $middlewareGroups = [
    'web' => [
        \App\Http\Middleware\EncryptCookies::class,
        \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
        \Illuminate\Session\Middleware\StartSession::class,
        \Illuminate\View\Middleware\ShareErrorsFromSession::class,
        \App\Http\Middleware\VerifyCsrfToken::class,
        \Illuminate\Routing\Middleware\SubstituteBindings::class,
    ],

    'api' => [
        'throttle:api',
        \Illuminate\Routing\Middleware\SubstituteBindings::class,
        // Add ExportCsv middleware to all requests. "data" is the name of
        // the key to retrieve using "dot" notation.
        'csv:data',
    ],
];

protected $routeMiddleware = [
    'auth' => \App\Http\Middleware\Authenticate::class,
    'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
    'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class,
    'can' => \Illuminate\Auth\Middleware\Authorize::class,
    'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
    'password.confirm' => \Illuminate\Auth\Middleware\RequirePassword::class,
    'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class,
    'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
    'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
    'csv' => \audunru\ExportResponse\Middleware\ExportCsv::class,
];

class ProductController extends Controller
{
    public function csv()
    {
        $products = Product::all();

        return $products->toCsv('filename.csv');
    }

class ProductController extends Controller
{
    public function csv()
    {
        $products = Product::lazy();

        return $products->toCsv('filename.csv');
    }

php artisan vendor:publish --tag=export-response-config