Laravel Excel Export Import | Laravel tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
thank you Hello friends welcome in this video I'm going to work with laravel Excel I had many comments in my videos or we can work with Excel and I'm going to use these Pockets now to export and import Excel files with laravel project I am inside the Windows machine now I'm going to use a sale so let's go first and create the project I'm going to open my terminal here and let's paste that command and I'm going to change the name of the app to be let's say a lot of lxl okay that hit enter now and it's going to create the project very good next let's go to the package documentation and installation and we need to run this command text to install the package or if the composer require fails on the level 9 because of the simple cache we can also install that by running this one so let's copy this command now and let's open the terminal and we need to add the password Here and let's see the in the laravel Excel and first we need to say sale up the start the sale and I'm going to open a new terminal and let's navigate in the project and now let's say sale and paste the command to install the simple cache and the Excel package insert install that now Also let's open the project in here by just adding the localhost and here we are I'm going to open the project with the vs code and let's say here code dot okay here we are let's go in the dot dnv file and we have this laravel underscore Excel or database name and I have created this database so you go and create that and then let's run the artisan so sail artisan migrate I'm going to say my great brush and also dark Dash seed and just before we run in let's go in the Cedars that are they Cedar and uncommon that is and say user Factory to create 100 users save that and now let's run the command OK setting the database and if I open the database here refresh okay now here we have a 100 users very good the first thing I'm going to do I'm going to add a form here to import the Excel a button to export the Excel and also a table to display all the users so for that let's go in the vs code inside the resources view so welcome first I'm going to remove the Styles we have by default and I'm going to add the title in CSS through the CDN so by adding this align here and also let's remove everything very good and let's add a div with the class the B Max width of a seven Excel MX Auto okay and inside here I'm going to add another div with the class sorry with class to be Plex and justify between okay and let's add also margin two and padding two and inside of this div I'm going to add the Pearl and also the link the export so anger tag export that after that uh div I'm going to add a table to display all the users so here I have a table and I'm going to copy this table and we cannot hear but I'm going to create a component for that so let's create the component through the terminal let's clear this and I'm going to say sale artisan make component and let's name it a users table okay and now we have this inside the components we have this user's table list based the table inside and now here we need to add that components of x dash users Dash table and I'm going to select close save and if I open now the project and refresh yeah we have the table just need let me add in the body a class of BG I think Gray 900. very good now as I said here I want to display all the users so for that I'm going to create a controller first so let's say artisan make controller and let's give it a name I'm going to name it Excel controller let's open the vs code and inside the roads web Road let's remove the closure here because I'm going to use that controller we created so Excel controller class and the method for that is going to be welcome and let's give it also the name just in case they have here save this and now let's go inside the welcome the controller and create the welcome method so public function welcome here let's get all the users which is going to be equal with user model all static method and then return The View welcome and add all the users here let's save and now inside the welcome we have the users let's add here so users it's going to be equal with dollar sign users we have from there save open this table and we need to make a loop here I'll just let's remove this D hat and remove this one we need only one here this one I'm going to cut and I'm going to say make a forage or for us users as dollar sign user and paste that in just remove this as well and here I'm going to display the ID so let's say dollar sign user ID here the name dollar sign user name and here the email so you can add also other things but I'm going to add only the ID name and the email let's remove and let's save now go to the browser and refresh and here we have all the users 100 users scroll down okay now let's add the link here a button to export that but before we need to go here and go to the exports and here we have a quick start we need to make export and here we have an example for users that's it okay for us so copy that and in the terminal we need to use sale so just copy the artisan and come here and say sale and paste that command in and then in the controller we just say return Excel download new and the instance of a user's export and give it a name so let's copy this and go here and create first the controller the road sorry Road get slash and I'm going to give it a name export the controller is going to be the Excel controller okay and the method is going to be export let's give it also a name here as well export and let's save this now here create the method public function export and we just need the return for this Excel and we need to import that so import from mat website slash Excel slash facades Excel okay that one and also import this very good let's save and now uh in the welcome when we have this anchor tag let's say export here and we need to add a strap where he's going to go and it's going to go to the road we created and we give it a name I think yeah well the name is export so let's say Road export and let's give it some classes the class with PX or py2 and BG indigo 500 on Hover it's going to be BG indigo 700 and text let's say text White and what we can say also the around that MD like this if I save and in the browser now refresh yeah here we have the export very good if I click and it's going to ask us to save this file okay and let's open now this file here and yeah here we have all the users with name the email and also the timestamps and this is because we just they know in the export we say user old here we got collection and we return everything good now let's work with the import if we go here we also have a some export formats for example Excel SSV and other things export tables from query because this we have from the collection as you can see here we also have from query or from View and I'm going to show you so from view here but just right now let's work with the import as well we need to create first the import so let's copy this command and let's navigate in the terminal and let's say sale and paste that command in and then we need to create the user like this so let's copy this and in the vs code here we have the Imports returning new user and paste that in now the name is going to be what we have in the first row as argument foreign first argument and the email in the row as a second argument but because I'm going to import what we exported here the first argument is the ID so I'm going to say here uh the second argument which is the name and for the email we are going to say the third argument and for password we don't have but we are going to add what we have in the that a base factor is user Factory yeah we have this one so let's add this thing right now but you can if you have a user you uh password you can do something like this just let me add that one and click save okay now then we in the controller we say public function import and let's copy this because we are going to do the same thing so in the roads of course we need to create the road which is going to be a post route so let's say a road post sorry okay let's create the road so Road post and it's going to go slash import and let's add the controller which is the Excel controller class and the method is going to be import as well and give it a name as well which is import now you can name what you like but I just and in here let's paste that public function import but here we need the request because we are going to get from the form the file dollar sign request and external import new users import and let's save this import this sorry and as a file we need to get from the request file so it's a request file and inside the files we we are going to name the file users or you can name it like and then redirect back redirect to the home page okay now in the welcome when you have a form let's work with that first let's say uh give it action the B of the road which is import we other name import for that yeah the method is going to be post and we need to add also the end type to be multi-part form data because we are going to use to work with files also we need to add the csrf token here and let's add the input with type file and uh let's give it a name users and let's close this and let's add now the button with a type submit and I'm going to add the same classes we have here okay I think we are okay now let's close and let's say import or this one save and if I refresh yeah we have something like this just let me uh to this one I'm going to add yeah we have justify between here and let's add a BG slate of uh 700. and the refresh and yeah now let's choose a file and I'm going to choose that one but we have a 100 users and we are going to create another 100. let's use a file and by the way if I click import we are going to have error so let's say are required here and if you want you cannot also validation so let's say require foreign and in the controller also you can say the request validate and validate the users to be required and now if I just say import we're going to read already right back okay let's choose a file and I'm going to choose this one click open and if I say import yeah we have duplicate entry because we have the same URL but what we are going to do we are going to clear that so let's say sale artisan and close the migrate press and right now if I just go to the home page we don't have any users but if I import the users from here and let's say import we have all the users we had before okay so that is how we can Import and Export and now just let me show you also for the export as from The View and here we have an example instead of from collection we need to save from View and let's go or users export where is the user export in here let's remove the from collection because we are going to use from View okay we import that and now we have the error because we say public function collection but and he says in a collection and he's expecting View and let's copy this and paste it here as well and we need to import The View from illuminate contracts View like this and what we need to import the return here not exports invoices but I want to export because it is is going to convert the HTML table into Excel and the table we have inside the components users table so let's say in here return The View which is inside the components Dot users Dash table and here we need to add all the users which is going to get from the user model all save that but just need to show you if I open here right now we have the ID the name the email and the timestamp but in the table we just return the ID name and the email and now when we are going to export also we are going to export only the ID name and the email what we have in the table here so let's say export let's say fresh first and let's say export okay save it here and let's open now this one and as you can see we have only the ID the name and the email let's run again the migrate brush okay go to the browser and if I refresh here yeah we don't have and now I'm going to import uh the user's one what we exported the last one from the View and let's say import we get back all the users again okay friends so that's it about this video how we can use this package for Import and Export excels okay friends that's it I hope you enjoy and see you in the next video all the best
Info
Channel: Code With Tony
Views: 9,446
Rating: undefined out of 5
Keywords: laravel, vuejs, livewire, laravel 8, laravel 9, laravel crud, laravel 8 tutorial, laravel 9 tutorial, laravel tutorial, laravel excel, laravel packages, laravel review, laravel tutorial for beginners, learn laravel, import export excel in laravel
Id: ISGVKpJ4StU
Channel Id: undefined
Length: 25min 57sec (1557 seconds)
Published: Fri Oct 14 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.