Getting Started

Summary: In this tutorial, I would like to share with you step by step export or download excel or csv file from database using maatwebsite package in laravel 6.0 application.

Step 1 : Install maatwebsite/excel In this step we need to install Maatwebsite package via the Composer dependency manager, so on your terminal fire below command: 

 composer require "maatwebsite/excel" 

Now open config/app.php file and add service provider and alias. 

 'providers' => [
     Maatwebsite\Excel\ExcelServiceProvider::class,
  ],

 'aliases' => [
    'Excel' => Maatwebsite\Excel\Facades\Excel::class,
  ],

Step 2: Create Export class and Model: Now ,we will create export class binding with the model by using this command : 

 php artisan make:export UserExport --model=User 

Note: You can modify the export class according to your requirement , for example- 

 namespace App\Exports;
 use App\User;
 use Maatwebsite\Excel\Concerns\FromCollection;
 use Maatwebsite\Excel\Concerns\WithHeadings;
 class UserExport implements FromCollection,WithHeadings
  {
     /**
      * @return \Illuminate\Support\Collection
     */
     public function collection()
      {
        return User::select(['name','email'])->get();
      }

      public function headings(): array
       {
          return [
            '#',
            'name',
           ];
        }
   }

Step 3: Add Routes:

 In this step, we need to create route of export file. so open your "routes/web.php" file or "routes/admin.php" file and add following route.    

Route::get('/showdata', 'CsvController@showdata');     
Route::get('/export', 'CsvController@export'); 

Step 4: Create CsvController.php:

In this step, now we create new controller as CsvController.php in this path ,for which above routes have been defined "app/Http/Controllers/CsvController.php". 
Command to create Controller : 

php artisan make:controller CsvController 

Now,modify CsvController by the below code :

  namespace App\Http\Controllers; 
  use App\Exports\UserExport; 
  use Maatwebsite\Excel\Facades\Excel; 
  use Illuminate\Http\Request; 
  use App\User; 
  class CsvController extends Controller 
  {
    public function export(){
      return Excel::download(new UserExport, 'users.csv'); 
    } 

    public function showdata(){
      $data = User::all(); 
      return view('users',['users'=>$data]); 
    }
  } 

Step 5: Create Blade File Here,in this example we are creating a view file "users.blade.php" on path "resources>view" as-

 <table>
            <thead>
            <tr>
                <th>Name</th>
                <th>Email</th>
            </tr>
            </thead>
            <tbody>
            <a href="{{url('/export')}}"> test</a>

            @foreach($users as $user)
                <tr>
                    <td>{{ $user->name }}</td>
                    <td>{{ $user->email }}</td>
                </tr>
            @endforeach
            </tbody>
  </table>

 

Step 6: Create Dummy Records:     
         If you are not having enough data you can create dummy records using    factory() in DatabaseSeeder under path "Database > seeds > DatabaseSeeder.php" as-  

  use App\User; 
  public function run() { 
     factory(User::class,20)->create(); 
  }

Then,run command :

php artisan db:seed  


Now you can check on your laravel 6 application with demo file for testing. I hope it can help you...

Comments

Sign in to publish a comment

0 comments

Be the first to comment on this post.