DEV Community

Cover image for How To Import And Export Excel And Csv Files In Laravel 12
Msh Sayket
Msh Sayket

Posted on

1 1 1 1 1

How To Import And Export Excel And Csv Files In Laravel 12

In this example, I will show you how to import and export excel and csv files in laravel 12 application.

We will use the maatwebsite/excel composer package for import and export tasks. In this example, we will create a simple form for input where you can upload a CSV file and create multiple users. Then, I will create an export route that will download all users from the database in an Excel file.

So, let’s follow the steps below to create the import and export function in a Laravel 12 application. You can export files with .csv, .xls, and .xlsx extensions. You Can Learn How to add form validation in laravel 12 Example

How To Import And Export Excel And Csv Files In Laravel 12 Example

Step 1: Install Laravel 12

This step is not required; however, if you have not created the Laravel app, then you may go ahead and execute the below command:

laravel new example-app
Enter fullscreen mode Exit fullscreen mode

Step 2: Install maatwebsite/excel Package

In this step, we need to install the maatwebsite/excel package via the Composer package manager. Open your terminal and enter the following command:

composer require maatwebsite/excel
Enter fullscreen mode Exit fullscreen mode

Step 3: Create Dummy Records

In this step, we will create some dummy records for the users table so we can export them later. Let’s run the following Tinker command:

php artisan tinker
Enter fullscreen mode Exit fullscreen mode
User::factory()->count(10)->create()
Enter fullscreen mode Exit fullscreen mode

Step 4: Create Import Class

In Maatwebsite version 3, a way is provided to build import classes, which need to be used in the controller. It would be a great idea to create a new Import class. So, you have to run the following command and make the necessary changes to the code in that file:

php artisan make:import UsersImport --model=User
Enter fullscreen mode Exit fullscreen mode

Read Full Tutorials

Quadratic AI

Quadratic AI – The Spreadsheet with AI, Code, and Connections

  • AI-Powered Insights: Ask questions in plain English and get instant visualizations
  • Multi-Language Support: Seamlessly switch between Python, SQL, and JavaScript in one workspace
  • Zero Setup Required: Connect to databases or drag-and-drop files straight from your browser
  • Live Collaboration: Work together in real-time, no matter where your team is located
  • Beyond Formulas: Tackle complex analysis that traditional spreadsheets can't handle

Get started for free.

Watch The Demo 📊✨

Top comments (0)

AWS Q Developer image

Your AI Code Assistant

Automate your code reviews. Catch bugs before your coworkers. Fix security issues in your code. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay