Laravel How To Upload,View And Download Pdf,Docx,Mp4,Mp3 In Laravel Laravel 8 Tutorial From Scratch

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone welcome to f tech knowledge this is yamin and in this tutorial i am going to show you how we can upload pdf doc file video or mp3 in laravel as well as i will show you how how you can view those file and how you can download those pdf video or mp3 okay so let's get started and as you can see this is my laravel project i already opened it okay and this is the folder project folder okay and i'm going to use sublime text for this tutorial and i already opened this project over here and the view you are seeing is coming from welcome.blade.php okay from here so first of all i am going to add a button over here and if we click it it will take us to another page where we can upload some data in the database okay so let's get started so for creating button i'll use anchor tag and i will server here upload page and we will use a bootstrap class or over here we'll say btn btn success okay so let's save it and let's refresh okay we right now have a button so let's uh add a url over here so we'll say url let's say upload page okay so let's save it now we need to create this route okay so let's go to route and web.php and i'm just going to copy it the route will be get and our urls uh upload page right i will copy it again and paste over here and we need to give a function name so let's say let's say upload page okay our function name will be upload page as well so let's save it now we need to uh go inside this controller and create this function okay and we are using uh laravel version 8 right and we saw we need to add controller over here in the route okay so let's go to our controller app then http then controllers over here you can see page controller okay so i'll just copy it and paste over here and our function name was upload page right so i'll copy it and paste over here and over here we'll say let's say our page name will be product okay so let's say product so let's save it control s now we don't have this product.blade.php okay so we need to create it so i'll go inside view right click new file then product dot let dot php control s okay save so let's just write some random things and check if it's work or not let's all have success uh inside each one so let's save it save our hair save over here and okay so let's refresh okay we are getting the view okay so let's uh create our head some input field where you can upload some data in the database so for it i will say input input type text and name equal to name and placeholder equal to let's say uh product name product name okay so i'll again copy it control v and let's say input type text and this time name call description and over here placeholder this caption okay and we will have a input type for file input type file and the name will be file you can give name whatever you want and a submit option input type submit okay and okay let's save it and let's check how it looks okay it looks like this i'll just zoom it a little bit for you okay so it looks like this and over here let's create form [Music] okay let's copy it ctrl x and paste it over here and over here we need to say the action type and then the method and since we are using file so we need to say enc type which is multipart form data okay and the method will be post since we we are sending data to the database and we also need to use a token which is csrf field okay and over here we need to add a url so for this we will say url and inside it uh let's name it upload product okay upload product so let's save it ctrl s okay now we need to create this route in web.php so i will again copy it and paste it over here and this time i will say post because we are sending our twitter database and our url name was uh upload product right so i'll copy it and paste over here and let's say this time uh our function name let's say store so let's save it now we need to create this uh function in our controller so let's go to page controller i'll again copy it paste over here and i'll show over here store and then we don't need to return any view so this time okay we also need to save over here request since it's it's a post request dollar request okay control s now let's check if we are actually can get all the data in our controller so for it uh we'll just write return dollar request okay so let's say we ctrl s so let's refresh now let's uh okay on both okay yummy let's say product name in in let's choose a file from here submit okay we can actually get all the data okay [Music] so now the thing is uh we are passing the data in our uh page controller right now we need to create a table where we can save all those data right save all this data so we need to create a model and a migration so for it we need to go to our project then click shift and right click on your mouse and open partial window here now for creating model uh we need to say psp addition make model and then our model name okay okay it's taking some time so let's wait we need to say php rt sun make model sorry make model that's our model name will be product now if we give minus m it means it will also create a migration in our database okay so let's press enter so it will create a model a product model and a product migration okay our model and migration created successfully let's go back to our code and if you go over here model you can see product.php which is our model okay and then if you go down here uh inside database then migration uh you will see a product table okay this is migration product migration over here we'll add dollar table string then we have name name field then we have description field and then a file fit okay so let's save it ctrl s now we need to uh write one more command which is php artisan migrate okay then before that i want to show you my database now if you go inside dot env you can see that my database name is best a laravel basic okay and over here this is my database laravel basic and over here you can see that uh i only have uh six table over here we don't have productive over here okay so over here i'll just say php rt sun my great if i press enter a table will be created in our database okay a table is created now if i refresh over here you can see a product table over here if i click it you you can see name description and file okay so let's go back to our code and let's go back to our controller and over here uh i will just copy a little portion of code okay over here uh i am i just uh paste a little code over here we are actually requesting a file from our product.blade.php okay and then uh we are giving it a name which will be like which will be like 1234.pdf okay something like this then uh we will be uh save our file inside public inside asset okay so i already create a uh asset folder inside my public i'll show you a cover here you can see asset folder over here okay and all the uh all the document like pdf or video will be saved inside this asset folder okay and then we are saving the data in our database okay now right now we need to say dollar data equal to our model name which was product sorry yeah which dollar data equal to sorry new product okay now we also need the i need need to add this model over here otherwise it don't work so we need to say app then backslash you can see over here our model is inside app okay model models then again backslash our model name was product okay so now uh we will say dollar data name is equal to dollar request name and again i'll again copy it and paste it now this time description okay then i will say dollar data save then return redirect back okay so ctrl s let's save it and let's go over here and refresh upload paste then let's say uh food okay let's just say name over here then description blah blah blah now let's add a file now submit okay our data is submitted let's check our database okay guys and here you can see all the data that we just uploaded right now okay so uh uh first of all let's uh create a view to show all those files okay so let's go back to our code again now let's uh go back to route and let's create a view i mean uh okay over here let's say get method and over here uh we will say show and then let's say function name will be show as well okay so let's save it ctrl s now let's uh go to our page controller let's over here public function show and we'll just return a view over here okay we will say first of all we need to declare a data okay data equal to our table name which was product all okay and then we are going to return view return view let's say our view name will be a show product and comma compact data okay so we need to create this view right now which will uh show all the product from the database okay so let's go to views then a new file show product dot blade dot php so ctrl s save okay so html and over here we will say at the rate of for each dollar data as dollar data and then at the rate of e and d for each okay now inside it uh we let's say uh tr and over here let's uh th let's copy it and paste it let's about here name then address encryption then file okay so let's create another tier and inside it uh we will have all our data okay so td and sorry uh our for each loop will start from here actually after table header and over here i will say dollar data name then over here this description and file okay so let's save it ctrl s and let's see we can show all the data and our name will show okay so let's see what here show okay we can get the data name description file which is coming like this so we need to um remodel it actually let's go back to our code and over here let's get a little design let's overhead table i'll just cut it from here and paste it before in for each and over here i will say border sorry border uh on px so let's save it and let's refresh okay we now have the file now over here we can see the view file name over here right now we don't need to see the file name okay so we will i have a view option over here and the download option over here okay so let's go to our code and let's over here view and download okay download so i am going to cut it and over here i will use anchor tag okay now inside this anchor tag i will say view and then href is equal to okay let's keep the href later on ctrl c ctrl v and over here we'll say download let's save it and let's refresh again okay we right now have a view and a download option over here okay so let's uh work with the let's say download first okay let's work with the download first so let's say let's give it a url url then slash let's say download now over here we need to pass our file so i will say dollar data file okay so ctrl s so we need to create this route download so ctrl c ctrl v so it will be get request and our your loss down load and we need to add the file over here file file control s so uh where is it okay this one so it will send off our file over here in our uh in this route and our route will send the file to our controller so let's go to our controller and we also need to give it a name so we will see over here download okay download control s so we need to get this function in the controller so let's see over here i'll just again copy it ctrl c ctrl v and now it will be download and it will be request dollar request re q ust comma dollar file okay control s and uh this will be a post request sorry this will be a post request i think and let's cover uh controller let's go to our controller and let's remove this and over here i will just uh copy paste the code i'll just copy this line from here ctrl c ctrl v okay what it does is uh it will download the file from our location our location was asset dot file okay so it will uh download our file uh form you can see that we are saving all our file inside asset right so it will download the file that we want to download so let's save it and let's go over here let's start from the beginning okay let's just add another data let's say random things over here then choose a file this time we will choose this one okay open now submit okay i thought i submitted and now let's go to our show page now you can see our two dot over here okay okay guys i think i know where i need to make the changes i think it's in the show product over here the table ending will be after the in forage okay so ctrl v ctrl s now if we refresh it will show properly okay now let's see uh okay now let's just click download let's see we can download or not okay page not found let's check it again actually uh actually guys uh we need to use a namespace over here like you need to use uh we need to write use i'll just copy it actually control c ctrl v we need to use uh this on over here support slash if we see it packets slash backslash short is shortest okay we need to use this over here because we want to download file right so for downloading file from the uh from the laravel shortest and public folder we need to use this line and one more thing i think it will be a get request instead of post okay i uh it was previously post okay i changed it to get okay so let's save it and save it so this time if we refresh it should work perfectly so if we click download uh it will download as you can see download a pdf if you click this it will again download another pdf as you can see so right now let's work with the view so for this uh let's go to show product and over here inside the view hdf we will use another url and see over here let's say just view and then we will send a id with it okay dollar id so let's save it let's go over here and again let's copy it and let's paste over here let's over here view and it will be id and let's see our hair view okay so let's save it control s let's go to page controller now over here we'll create again another function public function view then we will catch the id over here we will say dollar id then we will say uh dollar dollar data is equal to our uh model name which is product product then key find dollar id okay then we just return a view return view let's say this one name is view product okay comma compact with this data with the specific data okay comma compact data so let's say we control s now we need to create this view view product i'll just go inside view and then say view product dot let dot php control s save it and over here let's just say html and inside body uh let's say let's just get the data i mean the name and description so dollar data name ctrl c then this time description control s and now to show our file we will use iframe and inside iframe we need to grip uh give the file location asset i think i said uh backslash dollar data file okay so let's save it and let's refresh over here and if we click view right now okay you can see that uh it's showing my name the description and file not found okay so let's check it again over here dollar data file okay guys let's go back to our view product and over here we need to give a slash something sometime it creates a problem okay so let's save it and let's refresh again hope this time it works okay it's up perfectly this time so let's uh give it a more height and width let's say height will be 400 and width will be 400 okay so let's save it and let's refresh okay as you can see is hidden with chains okay so let's uh so let's upload a video right now okay so over here let's say let's go back to over here let's click upload page this time we'll upload a video okay so let's say video and description is video and let's choose a video i have a video uh over here now uploading a video file uh actually might take some time it actually depends on your video file size so it will take some time so just wait a little bit passionately okay okay our video is uploaded so let's uh go to our view not this view sorry yeah let's go to our show actually show over here we can see uh over here this one is video right so let's click view over here you can see that the video is over over here so if you can also play it as well you can see this there is a bar over here okay so let's go back and if you want you can also download it as well so i will click it and it will start downloading okay as you can see so thank you very much for watching this video i hope you learn how you can uh upload any kind of file in laravel and how you can view them and download them as well so thank you very much for watching and don't forget to give this video an video a like and subscribe my channel as well thanks again
Info
Channel: Web Tech Knowledge
Views: 9,917
Rating: undefined out of 5
Keywords: download in laravel, file upload in laravel, upload file in laravel, laravel file upload and download, laravel pdf download, download file laravel, laravel download file, laravel 8 pdf download, pdf file download in laravel, laravel file download, How To Upload View And Download Pdf Docx Mp4 Mp3 In Laravel, upload and download video/mp4 in laravel, upload and download audio/mp3 in laravel, upload and download docx files in laravel, view pdf docx video in laravel
Id: IYswY0Jgup4
Channel Id: undefined
Length: 35min 28sec (2128 seconds)
Published: Tue Mar 23 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.