How to Export data to Excel file with different format in Laravel 10 with example step by step

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys welcome back so guys in this video we are going to learn how to export data to Excel file in LL okay so guys in previous video we have seen about how to import the Excel file into database in LL okay so where we have seen about the installation and file setup of that package that is larel Xcel and also known as Matt website package okay so now guys let me just go to that website here so this is a l xcel.com which has a matte website package yep and now guys this is the introduction part and if you have not installed in your application you just need to follow up this uh installation process like install the package that is Matt website Excel then set up your configurations and publish the vendor files so if you don't know how to install it I have written a post you can just follow that post link given in the description or else you can follow this tutorial over here okay so now guys let's get back and continue with the export data to Excel file so now guys I have created a crowd application which is about the customers so here is my list of customers and I want to export this data in a Excel file so how can you achieve this so guys let's get started so let's move to our editor yep and let me just zoom it so now guys First Step let's begin with creating our customer export okay so yep now you can go back to the website and here you can see the export tab in the sidebar and here is a 5 minute quick start and here is a command guys that is Artisan command to create a Excel export data okay so now guys let's copy this and you can paste in your terminal by right click on the mouse and change your command here like instead of user users we are going to tell it is customer okay so hit enter file is created okay so you can follow that in app and here you find that exports and here is your customer export so guys here you see uh by default it gives me the form collection and here you can directly paste your code as shown in this documentation example so here you can directly return your model so let me copy this and paste here so return model and instead of my user model I'm going to return return my customer model so import your class here which is my model let me just show you that customer. PHP so this is my customer detail guys okay let me close this customer model yep and now next step let us see what it is here it is about the controller part so where you are going to write this logic of controller to download the Excel file okay so now you have to just copy this and get back and you have to create One controller so guys I have already created one controll controller which is about the customer controller so here HTTP controller and where is my controller here customer controller perfect and yep let's paste this and now guys at this function here at export function you have to call your created export class so which we have created just now about the customer export so let's type here customer export okay done and you have to import it class so you just import this okay which is app export and customer export okay done so once setup is done guys now we have to create a URL so we can download the customer report so before that I just want to keep this in a separate variable here dollar file name equal to and paste Here and Now guys let us copy this file name and and paste here so guys our controller setup also is done and now what we have to do um you need to create a route so we can export the data for that so let's copy this and get back so go to your web.php routes web.php and create a route so let me just paste that contrl V and you need to go to your controller which you have created so I have already created One controller which is customer controller so let me copy that path here and paste that name space here and back slash and customer controller class and which goes to the export function so you are going there okay so let me click on it and you reach here and this does your Excel download file so with this Excel download it downloads your customer data okay so which is inside this class let me just reshow you again here so in this collection you see that we are returning the customer model so customer model will get your table data okay that is your database table records it will get an export in a Excel sheet now get back to your controller and here guys I just want to mention that you have to import this class so the class is about this which is Matt website Excel fet Excel okay yep let's get back and now guys you can copy this uh route so instead of User make as customer export that's it now let's copy this and wherever you are on the browser just serve that and paste here so it will download the customer file okay and now guys let's hit enter so it will download it perfect so here you see that your Excel file is downloaded so instead of user you need to tell it's customer data so we have not changed that let me go back to our controller so here here you see that it's user so instead of user let's mention customers okay now let us try to redownload it customer for/ exort and hit enter so here you see again it's downloaded perfect so let's open this file guys cool so you see that all the record has come so we have total eight records in our database let me just show you that so let me Zoom this and get back to your database and here you see we have total eight records in our database that is downloaded in this Excel sheet perfect okay and now guys uh let us see how we can add a heading columns inside this Excel sheet so right now you see that we have directly exported the data in Excel seat without the headings so how can you achieve that so let me just tell you let's get back to our Export customer export so here you see that app exports and customer export so let's go back to our documentation and you'll find a mapping data inside this mapping data you have adding a heading row okay so you can utilize this function let's copy this get back and paste here and with this you have a withth heading so just add that comma and paste and and then you can copy this import class paste it so we have imported that with heading and now I don't want to get all the data like here you have some date about it and here updated at column this IDs I don't want I just want this name email and phone number so let's get those datas only so here in my heading I will add name then email email and then phone and then guys let's change this query also so instead of all let's use get and before that add a select option and inside this select mention your column field names like what are columns you want so I just need this name email and phone so get back name comma single codes then email comma single codes and then phone done and now guys you can get back and let's export data once again so customer SL exort hit enter so you downloaded one more file which is customer one let's opening perfect so here you see guys we have exported with the heading that is name email phone and here you have other eight okay so now guys you have seen about how to export the data with heading also so you have learned two methods let me close this Excel files and now guys let us see one more method of downloading the Excel sheet that is using the blade view so you can see here and here you can see that it is from view so it mentions that you can use the blade file to export your data so guys what you can do you can just copy this and get back to your customer export and let's comment all this code because we do not require any of the code inside this and just mention one function that is view okay and yep let's get back and here you see we have form view get back and you can just remove this guys like um for reference let's comment and keep it you can just keep at top for your future reference and let's import this View and form view both the classes let's copy and paste here okay done and guys if you want you can just comment this again so you do not require like form collection and with headings so here you need to change the format like when you view you have to go to One customer folder and go to a export blade file and inside that we'll be creating a customers variable okay I mean we will be accessing it and here you need to call your customer model so in case your might be uh order items or anything else and now guys you need to create a blade file named export in your customer folder so let's go to Resource views and inside views you have to create a folder called customer so have already created so let's right click and create a new file export. blade. PHP so file is created and now guys in this you don't have to do any extension or ex extending your blade layouts so it's very simple just paste your HTML template table template okay so let's copy this and paste here done okay and now guys you have to just change your uh column field requirement so this will be my heading area like in the Excel sheet we have a column right in top like name email and phone so this will be my column headings and our and then this for will be all other row CES okay so from where are you getting this variable so you just getting from your export file okay so let's copy this so you click here and you reach there and then let's paste here customers and then guys you can just rename this as item or just mention customer and change other fields so name email and phone cool so now guys you can get back and try again with that same like customer forward SL export so here you see um we have got that customer to again so yep so you get this Fields guys so if you want to uh just differentiate it is coming from the bleed file or not you can just add your like uh name blade okay so this is just to confirm whether we are downloading from this blade file only or not yep so give export so you'll see now the file as name blade okay so here you see name blade has come cool let's close it done so let me remove it and let's close this blade file guys so guys we have seen about three ways of exporting data so one more thing I just want to teach you about exporting your file formats so let's go to the documentation and here you see export formats so what is this export format so export format is nothing but downloading your Excel file in different extension like CSV or xlsx and tsv XLS you have many things okay so you can just go through this so guys I'll just teach you with the three options so how can you achieve that so guys first I'll be creating one form here a simple form with one extensions and one download button so let's get back and yep let me go to my web routes and here is my customers route guys okay so this is my customer get request which goes to the index function and in the index text function I have a blade file basically you just go to any form and add that form tag okay so I'll create one row then column md4 or five and inside this just create a simple form let me close this terminal yep so now guys just type your input input group margin top three and here use a method get and action to your what you have created the route for export your customer details so let me copy this so this is the customer forward SL export let's paste it done and now guys use a select here form hyphen control and option um select select type again option like xlsx then copy and paste CSV and then XLS so you can mention this in a lower case CSV and SLS X and now let's create a button which will be like export or download okay make this a submit button type name equal to uh we do not require name attribute here just design this button using bootstrap class button success and then guys here in the select name attribute you have give to type so we can understand what type of file you want to download okay so once this submits it goes to the URL customer for/ export so let's go to that route and here it goes to the export function so let's move there and inside this export function you are getting some requests like request of dollar request and with this help of dollar request you'll get your type so this type is nothing but your select name attribute type okay so you're are copying that and pasting here and then guys we'll mention the extensions type like uh double equal to your xlsx okay so let us keep this in a if else condition wise so if this comes then else again if so let me add in one line so you can again copy and paste and finally the else part let pasted here and CSV okay and finally in the else condition we'll keep a default extension so now guys let me create a variable called extension extension equal to and double codes just paste this extensions let's copy and paste again here which is going to be CSV copy and paste for the SLX and finally the same I want to keep here also okay and now guys uh let us see how we can achieve this exporting part so here you see you find a difference here so one more parameter you have to pass that what type of file you are using let me just zoom and show you okay so let me copy this and get back and paste create one export format equal to and directly paste it okay so don't use single codes or double codes just paste that and then guys it's a CSV so you have to mention that here you see like CSV so paste it and then for the XLS you find here copy and paste done so now let me copy this also and paste here so here at the last at file link let us just contrl s X and remove everything just concatenate so this will be my customer and I want to concatenate with the date also so you can use date like on what date you have downloaded date month and year and then concatenate with your Dot and then extension extension that's it so your customer date do XLS or CSV or whatever extensions come here so your f file name is generated and then guys after the file name pasting you have to give your third argument or parameter so let's copy this variable and paste here okay so if anything matches it is just going to use that format and paste here okay so now guys let us get back and refresh so here you see we have the type let me just give some space go to your plate file margin bottom four oops so mb4 refresh okay let me select the xlsx export or download so here you see that it has downloaded okay so the first format is downloaded and then guys let us select with the CSV export and you see that you have downloaded the CSV format okay it's not shown let me see why here it you can see that okay so let me just open and show you cool now you can close it and then forward with the SLX so you'll be finding that also let me just show you so here you see that XLS cool so guys we have successfully learned how to export the data into Excel file in Lal 10 okay including with the export formats okay so guys in this video that's it thank you for watching this video guys please subscribe like and share
Info
Channel: Funda Of Web IT
Views: 3,397
Rating: undefined out of 5
Keywords: fundaofwebit, laravel excel export date format, how to export data in excel file using laravel, How to Export Data to Excel file in Laravel, How to export database table to excel file in laravel, Export an existing view to Laravel-Excel, How to Export data to CSV & Excel format in Laravel, export excel file maatwebsite laravel, how to export data in excel file in laravel, how to export data from database in excel sheet in laravel, export data to file step by step in laravel
Id: 96kIYuWtQns
Channel Id: undefined
Length: 20min 36sec (1236 seconds)
Published: Wed Jan 24 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.