In Part-224 of the Laravel E-commerce series, we will work on the Export feature to export the users in CSV format.
We will use maatwebsite 3.1 package for the export functionality of users that we have installed in last video.
Install Maatwebsite package 3.1 version (if not installed yet) by running the below composer command:-
composer require maatwebsite/excel
1) Create usersExport
First of all, create usersExport class by running below artisan command:-
php artisan make:export usersExport
You can open usersExport file in which you can see the collection function automatically created that will return data from the User model.
2) Create Route:-
Now create Get route to export users in web.php file like below :-
// Export Users
Route::get('export-users','UsersController@exportUsers');
3) Update users.blade.php file:-
Now update users.blade.php file to add export users link.
4) Create exportUsers function:-
Now create exportUsers function in UsersController and write code to download excel file for users.
6) Add Header Statements:-
Add below statements at the top of UsersController to include usersExport that we have created and Maatwebsite class for sure.
use App\Exports\usersExport;
use Maatwebsite\Excel\Facades\Excel;
7) Update collection function :-
Now we will update the collection function in the usersExport.php file to write query to select users that are active only.
Maatwebsite Package 3.1 does not show column headings by default. We need to take below steps to add headings for our excel data means the users table column names so that it will be easier to understand the data.
i) Add below header statement for headings in usersExport file :-
use Maatwebsite\Excel\Concerns\WithHeadings;
ii) Update usersExport class in usersExport file like below :-
class usersExport implements WithHeadings,FromCollection
iii) Add headings function in usersExport file with heading names in array as shown in video.
This video is a part of my Laravel Advance E-commerce Series
►Click here to access the complete course: • Laravel Tutorial for Beginners | Adva...
►Click here to subscribe - https://www.youtube.com/stackdevelope...
Other Popular Stack Developers Series that can help you:-
►Laravel API Tutorial - • Laravel API Tutorial | Create API fro...
►Laravel Interview Questions - • Laravel Interview Questions & Answers...
►jQuery Tutorial - • jQuery Tutorial
►Laravel Basic E-commerce Series - • Make Admin Panel / E-commerce Website...
►Laravel Dating Series - • Make Dating / Social Networking Websi...
►GIT Tutorial for Beginners - • Git Tutorial for Beginners | Create y...
Follow Stack Developers on Social Media to get updates and resolve your queries
►Like Facebook Page to get updates - / stackdevelopers2
►Join Facebook Group to resolve your queries - / stackdevelopers
►Follow on Instagram - / stackdevelopers2
►Follow on GitHub - https://github.com/stackdevelopers
►Join this channel to get access to perks - / @stackdevelopers
Watch video #224 online without registration, duration hours minute second in high quality. This video was added by user Stack Developers 01 January 1970, don't forget to share it with your friends and acquaintances, it has been viewed on our site 519 once and liked it 10 people.