Cover Image for Import and Export CSV file in Laravel 5.8
186 views

Import and Export CSV file in Laravel 5.8

To import and export CSV files in Laravel 5.8, you can follow these steps:

Step 1: Install the Required Packages

Before you can work with CSV files in Laravel, you’ll need to install the maatwebsite/excel package, which is a popular package for working with Excel and CSV files in Laravel. You can install it via Composer:

Bash
composer require maatwebsite/excel:^2.1

Step 2: Configure the Service Provider

After installing the package, you need to register the service provider. Open the config/app.php file and add the following line to the providers array:

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

Step 3: Publish the Configuration File

Publish the package’s configuration file by running the following command:

Bash
php artisan vendor:publish --provider="Maatwebsite\Excel\ExcelServiceProvider" --tag=config

This command will create a excel.php file in the config directory.

Step 4: Create a Model (Optional)

You can create a model to represent the data you want to import or export. For example, if you want to import and export a User model, you can generate it with Artisan:

Bash
php artisan make:model User

Step 5: Importing CSV Data

To import data from a CSV file, you can create an import class. Generate an import class using Artisan:

Bash
php artisan make:import UsersImport --model=User

This command will create an UsersImport.php file in the app/Imports directory.

In the import class, you can define the mapping between CSV columns and model attributes, as well as any data transformation logic. Here’s an example:

PHP
namespace App\Imports;

use App\User;
use Maatwebsite\Excel\Concerns\ToModel;

class UsersImport implements ToModel
{
    public function model(array $row)
    {
        return new User([
            'name' => $row[0],
            'email' => $row[1],
            // Add more fields as needed
        ]);
    }
}

Step 6: Import Data from CSV

Now, you can import data from a CSV file in a controller or wherever you need it. Here’s an example of how to import data from a CSV file and store it in the database:

PHP
use App\Imports\UsersImport;
use Maatwebsite\Excel\Facades\Excel;

public function import(Request $request)
{
    $file = $request->file('csv_file'); // Assuming you have a file input named "csv_file"

    Excel::import(new UsersImport, $file);

    return redirect()->back()->with('success', 'Data imported successfully');
}

Step 7: Exporting CSV Data

To export data to a CSV file, you can create an export class. Generate an export class using Artisan:

Bash
php artisan make:export UsersExport --model=User

This command will create an UsersExport.php file in the app/Exports directory.

In the export class, you can define the data to export and any additional formatting. Here’s an example:

PHP
namespace App\Exports;

use App\User;
use Maatwebsite\Excel\Concerns\FromCollection;

class UsersExport implements FromCollection
{
    public function collection()
    {
        return User::all();
    }
}

Step 8: Export Data to CSV

You can export data to a CSV file in a controller or wherever you need it. Here’s an example of how to export data to a CSV file:

PHP
use App\Exports\UsersExport;
use Maatwebsite\Excel\Facades\Excel;

public function export()
{
    return Excel::download(new UsersExport, 'users.csv');
}

In this example, the export method will trigger the download of a users.csv file containing the data from the User model.

Step 9: Routes and Views

Don’t forget to create routes and views for importing and exporting data as needed.

That’s it! You’ve set up CSV import and export functionality in your Laravel 5.8 application using the maatwebsite/excel package. Make sure to customize the code to fit your specific requirements and design.

YOU MAY ALSO LIKE...

The Tech Thunder

The Tech Thunder

The Tech Thunder


COMMENTS