DEV Community

Cover image for How to import excel CSV file into Laravel
shani singh
shani singh

Posted on

13 2

How to import excel CSV file into Laravel

Import Laravel Excel

Import Excel CSV Files into Laravel 8

Today I am Going To Explain you about how you can import Excel / CSV Files into Laravel.

I am going to use Tech-Admin Panel for this.

For importing excel file i am using Laravel Excel.

Step 1 - Installation

To Install the Laravel Excel Package via composer run command below.



composer require maatwebsite/excel


Enter fullscreen mode Exit fullscreen mode

Next to export config file you need run command below.



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


Enter fullscreen mode Exit fullscreen mode

Step 2 - Create an Import Class inside app/Imports

Create Import Class by using artisan command



php artisan make:import UsersImport --model=User


Enter fullscreen mode Exit fullscreen mode

Step 3 - Update UsersImport Class

In order use CSV/Excel Files with heading we have to implement WithHeadingRow, UsersImport Class will look like.



<?php

namespace App\Imports;

use App\Models\User;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithHeadingRow;

class UsersImport implements ToModel, WithHeadingRow
{
    /**
    * @param array $row
    *
    * @return \Illuminate\Database\Eloquent\Model|null
    */
    public function model(array $row)
    {
        return new User([
            "first_name" => $row['first_name'],
            "last_name" => $row['last_name'],
            "email" => $row['email'],
            "mobile_number" => $row['mobile_number'],
            "role_id" => 2, // User Type User
            "status" => 1,
            "password" => Hash::make('password')
        ]);
    }
}



Enter fullscreen mode Exit fullscreen mode

Step 4 - Handle Uploaded Excel/CSV File



public function uploadUsers(Request $request)
{
        Excel::import(new UsersImport, $request->file);

        return redirect()->route('users.index')->with('success', 'User Imported Successfully');
}


Enter fullscreen mode Exit fullscreen mode

You can watch the explanation video for more clarity.

In Next part i will explain about Export Users.

Thank You for Reading

In case of any query related to LARAVEL.
Reach Out To me.
Twitter
Instagram

Neon image

Build better on Postgres with AI-Assisted Development Practices

Compare top AI coding tools like Cursor and Windsurf with Neon's database integration. Generate synthetic data and manage databases with natural language.

Read more →

Top comments (3)

Collapse
 
abdelqaouinabil profile image
AbdelqaouiNabil

what about gettiong also images from the cvs file

Collapse
 
shanisingh03 profile image
shani singh

You can put Image Path and Get that by File get Method of PHP.

Collapse
 
farirai123 profile image
Farirai Masocha

having problems uploading csv data to mysql database using laravel orchid

Image of Stellar post

🚀 Stellar Dev Diaries Series: Episode 1 is LIVE!

Ever wondered what it takes to build a web3 startup from scratch? In the Stellar Dev Diaries series, we follow the journey of a team of developers building on the Stellar Network as they go from hackathon win to getting funded and launching on mainnet.

Read more

👋 Kindness is contagious

Dive into this thoughtful article, cherished within the supportive DEV Community. Coders of every background are encouraged to share and grow our collective expertise.

A genuine "thank you" can brighten someone’s day—drop your appreciation in the comments below!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found value here? A quick thank you to the author makes a big difference.

Okay