How to Create CSV File from Custom Array using Maatwebsite ?

Share Me
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

Today now in this tutorial i will show you How to Create CSV File from Custom Array using Maatwebsite ?From here you can learn about laravel maatwebsite custom array. Now In this article, i will implement a custom array for create csv file laravel. Now i will explain this very simply step by step by using laravel maatwebsite custom array for create excel file. So it’s very simple example of how to create a csv file from array in laravel application.

This is a very simple and short example of how to create a csv file from array value in laravel by using maatwebsite package. We can use this example with any version laravel 6, laravel 7 and laravel 8 version.

So Let’s start and see bellow example:

Step 1: Install maatwebsite/excel Package

For this example we need to install the maatwebsite/excel package via the Composer package manager, so need to open your terminal and fire the bellow command:

composer require maatwebsite/excel

Now need to open config/app.php file and add the service provider and aliase.

config/app.php

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

'aliases' => [
	....
	'Excel' => Maatwebsite\Excel\Facades\Excel::class,
],
Step 2: Add Routes

Now in this step, we have to create a new route for export file. So just open your “routes/web.php” file and add the following route.

routes/web.php

<?php
  
use Illuminate\Support\Facades\Route;
    
use App\Http\Controllers\UserImportController;
  
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
  
Route::get('export', [UserImportController::class, 'export']);
Step 4: Create Export Class

Now in maatwebsite 3 version provide us a way to built the export class and we need to use in the controller. So it will be great way to create a new Export class. So we need to run the following command and need to change following code on that file:

php artisan make:export UsersExport

app/Exports/UsersExport.php

<?php
  
namespace App\Exports;
  
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;
  
class UsersExport implements FromCollection, WithHeadings
{
    protected $data;
  
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function __construct($data)
    {
        $this->data = $data;
    }
  
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function collection()
    {
        return collect($this->data);
    }
  
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function headings() :array
    {
        return [
            'ID',
            'Name',
            'Email',
        ];
    }
}
Step 4: Create Controller

So now in this step, now we need to create a new controller as UserImportController in this path of “app/Http/Controllers/UserImportController.php”. Now in this controller we will manage the export method, so put the bellow content in controller file as like in bellow:

app/Http/Controllers/UserImportController.php

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use App\Exports\UsersExport;
use Maatwebsite\Excel\Facades\Excel;
class UserImportController extends Controller
{
  
    /**
    * @return \Illuminate\Support\Collection
    */
    public function export() 
    {
        $users = [
            [
                'id' => 1,
                'name' => 'Sr sagor',
                'email' => 'srsagor@gmail.com'
            ],
            [
                'id' => 2,
                'name' => 'shahriar',
                'email' => 'shahriar@gmail.com'
            ],
            [
                'id' => 3,
                'name' => 'sagor',
                'email' => 'sagor@gmail.com'
            ]
        ];
          
        return Excel::download(new UsersExport($users), 'users.xlsx');
    }
}

So now we can check it on our laravel 8 application.

Now finally we are ready to run our this example. So just run the bellow command for quick run:

php artisan serve

Now we need to open the bellow URL on our browser:

localhost:8000/export

I hope it is working and you can learn.

Read Also :Laravel Join with Subquery in Query Builder Example

Thanks for read. I hope it help you. For more you can follow us on facebook.

About Shahriar Sagor

My name is Shahriar sagor. I'm a developer. I live in Bangladesh and I love to write tutorials and tips that will help to other Developer's. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by Shahriar Sagor →