CodeIgniter Laravel PHP Example Javascript jQuery MORE

Export data as excel file In laravel framework


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.
  2. composer require maatwebsite/excel

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

  3. Add providers and aliases in config/app.php
  4. 'providers' => [
    		/*
             * Laravel Framework Service Providers...
             */
            ......,
            ......,
            Maatwebsite\Excel\ExcelServiceProvider::class,
    ]
    'aliases' => [
        .......,
        -------,
        'Excel' => Maatwebsite\Excel\Facades\Excel::class,
    ]
    
  5. Now publish the changes using vendor:publish.

  6. php artisan vendor:publish


  7. Make migration and migrate it using command.
  8. Add routes
  9. /*Excel import export*/
    Route::get('export', 'ImportExportController@export')->name('export');
    Route::get('importExportView', 'ImportExportController@importExportView');
    Route::post('import', 'ImportExportController@import')->name('import');
    
  10. Create Import and Export class using import/export command
  11. Note: This command avaialble only if you download dependecy successfully using composer(step 1)

    php artisan make:export BulkExport --model=Bulk

  12. In app/Emports/BulkExport.php file
  13. 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),
            ];
        }
    
    }
    
  14. In Bulk.php Model
  15. <?php
    namespace App;
    use Illuminate\Database\Eloquent\Model;
    class Bulk extends Model
    {
        protected $table = 'bulk';
        protected $fillable = [
            'name', 'email',
        ];
    }
    
  16. create ImportExportController
  17. php artisan make:controller ImportExportController

     <?php
    namespace App\Http\Controllers;
    use Illuminate\Http\Request;
    use App\Exports\BulkExport;
    use App\Imports\BulkImport;
    use Maatwebsite\Excel\Facades\Excel;
    class ImportExportController extends Controller
    {
        /**
        * 
        */
        public function importExportView()
        {
           return view('importexport');
        }
        public function export() 
        {
            return Excel::download(new BulkExport, 'bulkData.xlsx');
        }
    }
    
  18. Create view file importexport.blade.php
  19. <!DOCTYPE html>
    <html>
    <head>
        <title>Import Export Example</title>
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css" />
    </head>
    <body>
    	<div class="container">
        <div class="card bg-light mt-3">
            <div class="card-header">
                Import Export Example
            </div>
            <div class="card-body">
                <form action="{{ route('import') }}" method="POST" enctype="multipart/form-data">
                    {{ csrf_field() }}
                    <input type="file" name="file" class="form-control">
                    <br>
                    <button class="btn btn-success">Import Bulk Data</button>
                    <a class="btn btn-warning" href="{{ route('export') }}">Export Bulk Data</a>
                </form>
            </div>
        </div>
    </div>
    </body>
    </html>