In Part-225 of the Laravel E-commerce series, we will work on the Export feature to export the orders in CSV format.
We will use maatwebsite 3.1 package for the export functionality of orders.
Install Maatwebsite package 3.1 version (if not installed yet) by running the below composer command:-
composer require maatwebsite/excel
1) Create ordersExport
First of all, create ordersExport class by running below artisan command:-
php artisan make:export ordersExport
You can open the ordersExport file in which you can see the collection function automatically created that will return data from the Order model.
2) Create Route:-
Now create Get route to export orders in web.php file like below :-
// Export Orders
Route::get('export-orders','OrdersController@exportOrders');
3) Update orders.blade.php file:-
Now update orders.blade.php file to add export orders link.
4) Create exportOrders function :-
Now create exportOrders function in OrdersController and write code to download excel file for orders.
6) Add Header Statements:-
Add below statements at the top of OrdersController to include ordersExport that we have created and Maatwebsite class for sure.
use App\Exports\ordersExport;
use Maatwebsite\Excel\Facades\Excel;
7) Update collection function :-
Now we will update the collection function in the ordersExport.php file to write a query to select orders that are active only.
Maatwebsite Package 3.1 does not show column headings by default. We need to take the below steps to add headings for our excel data means the orders table column names so that it will be easier to understand the data.
i) Add below header statement for headings in ordersExport file:-
use Maatwebsite\Excel\Concerns\WithHeadings;
ii) Update ordersExport class in ordersExport file like below :-
class ordersExport implements WithHeadings,FromCollection
iii) Add headings function in ordersExport file with heading names in array as shown in the video.
public function headings(): array{
return['Id','Name','Address','City','State','Country','Pincode','Mobile','Email','Registered on'];
}
In the next video, we will also show ordered products details in orders export to excel.
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 #225 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 505 once and liked it 7 people.