Laravel excel export custom query


In this example we will discuss about import excel file In laravel framework PHP.

Follow the below step to import :

  1. Download the dependecy using composer for Import and export excel file.

    composer require maatwebsite/excel

    You may visit https://packagist.org/packages/maatwebsite/excel for more details.

  2. Add providers and aliases in config/app.php

    'providers' => [
    		/*
             * Laravel Framework Service Providers...
             */
            ......,
            ......,
            Maatwebsite\Excel\ExcelServiceProvider::class,
    ]
    'aliases' => [
        .......,
        -------,
        'Excel' => Maatwebsite\Excel\Facades\Excel::class,
    ]
    

  3. Now publish the changes using vendor:publish.

    php artisan vendor:publish


  4. Make migration and migrate it using command.
  5. Add routes

    /*Excel import export*/
    Route::get('export', 'ImportExportController@export')->name('export');
    Route::get('importExportView', 'ImportExportController@importExportView');
    Route::post('import', 'ImportExportController@import')->name('import');
    

  6. Create Import and Export class using import/export command

    Note: This command avaialble only if you download dependecy successfully using composer(step 1)

    php artisan make:export BulkExport --model=Bulk

  7. In app/Emports/BulkExport.php file

    namespace App\Exports;
    use App\Bulk;
    use Maatwebsite\Excel\Concerns\FromQuery;
    use Maatwebsite\Excel\Concerns\WithHeadings;
    
    class BulkExport implements FromQuery,WithHeadings
    {
        /**
        * @return \Illuminate\Support\Collection
        */  
        // use Exportable;
    
        public function headings(): array
        {
            return [
                'Id',
                'name',
                'email',
                'createdAt',
                'updatedAt',
            ];
        }
        public function query()
        {
            return Bulk::query();
            /*you can use condition in query to get required result
             return Bulk::query()->whereRaw('id > 5');*/
        }
        public function map($bulk): array
        {
            return [
                $bulk->id,
                $bulk->name,
                $bulk->email,
                Date::dateTimeToExcel($bulk->created_at),
                Date::dateTimeToExcel($bulk->updated_at),
            ];
        }
    
    }
    

  8. In Bulk.php Model

  9. create ImportExportController

    php artisan make:controller ImportExportController

     
    

  10. Create view file importexport.blade.php

    
    
    
        Import Export Example
        
    
    
    	
    Import Export Example
    {{ csrf_field() }}
    Export Bulk Data


Laravel excel export custom query

Laravel excel export custom query

Laravel excel export custom query


Supercharged Excel exports and imports

A simple, but elegant Laravel wrapper around PhpSpreadsheet exports and imports.

Quickstart · Documentation · Video Course · Nova · Blog · Contributing · Support

Laravel excel export custom query

✨ Features

  • Easily export collections to Excel. Supercharge your Laravel collections and export them directly to an Excel or CSV document. Exporting has never been so easy.

  • Supercharged exports. Export queries with automatic chunking for better performance. You provide us the query, we handle the performance. Exporting even larger datasets? No worries, Laravel Excel has your back. You can queue your exports so all of this happens in the background.

  • Supercharged imports. Import workbooks and worksheets to Eloquent models with chunk reading and batch inserts! Have large files? You can queue every chunk of a file! Your entire import will happen in the background.

  • Export Blade views. Want to have a custom layout in your spreadsheet? Use a HTML table in a Blade view and export that to Excel.

Laravel excel export custom query

🎓 Learning Laravel Excel

You can find the full documentation of Laravel Excel on the website.

We welcome suggestions for improving our docs. The documentation repository can be found at https://github.com/SpartnerNL/laravel-excel-docs.

Some articles and tutorials can be found on our blog: https://medium.com/maatwebsite/laravel-excel/home

📬 License & Postcardware

Laravel excel export custom query

Laravel Excel is created with love and care by Spartner (formerly known as Maatwebsite) to give back to the Laravel community. It is completely free (MIT license) to use, however the package is licensed as Postcardware. This means that if it makes it to your production environment, we would very much appreciate receiving a postcard from your hometown.

Spartner Markt 2 6231 LS Meerssen The Netherlands.

More about the license can be found at: https://docs.laravel-excel.com/3.1/getting-started/license.html

Created by Spartner (formerly Maatwebsite)

We are a strategic development partner, creating web-based custom built software from Laravel. In need of a digital solution for your challenge? Give us a call.

https://spartner.software +31 (0) 10 - 7449312

🔧 Supported Versions

Versions will be supported for a limited amount of time.

VersionLaravel VersionPhp VersionSupport
2.1 <=5.6 <=7.0 Unsupported since 15-5-2018
3.0 ^5.5 ^7.0 Unsupported since 31-12-2018
3.1 ^5.8|^6.0|^7.0|^8.0 ^7.2|^8.0 New features