IMAGE UPLOAD LARAVEL - HOW TO UPLOAD FILES #laravel

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone in this video I will show you that how can you store images with the help of larval so as you can see I have designed these two simple Pages the first one is the product index page where you where all of the product will be shown from the database and the other one is the create new product for form page where from we will store all of of all over all of our products so as you can see this uh this form has four basic fields for our product the first one is the image name description and then price so so I choose these four basic fields for our product but the main thing is image we are working on storing image in this video so let's go to the application and in this application as you can see I have created a controller product controller and in this controller I have two function that is index and create function function so index is for returning the all all of the products into our index page and create is for the create form page now I have to store the data that is coming from our form we need to create a function here let's create a new function with a name store and this function will accept request and for now let's dye them all of the request that is coming from our form let's save this and after this we need to create a route for this function so let's create a new route and this will be post request because we are storing data into our database with the help of form let's call it product slash store and then let's call our product controller class and then the function that is store function and let's give this route a name product dot store let's save this now we can easily use this route into our form so let's go to the create create dot blade.php file and as you can see this is our form so let's use this route that we have created in our action let's call the route function and let's use this product dot store Route and the method for this route will be post because we have created a post request and after this do not forget to add csrf token into your form now let's go to our application and refresh the page so let's use fake filler to fill the data and lets you you choose an image and let's submit the form so as you can see our store Route is working we are getting our data from our form and as you can see I just have diadem all of the request data so as you can see all the data uh is died in our request but the main thing is image field we are just getting the name of the image that we are submitting with the help of form so this is not a proper thing we are we can get the proper image with the help of name so let's go to the uh form and whenever you need to store a image or any file with the help of form you also need to type in type attribute in our form and assign the multipart form data value so do not forget to add this attribute whenever you try to upload an image or any type of file with the help of form so let's save this now let's refresh this and if I submit the form with a proper file as you can see in our image field we are now getting the complete data of our image in a proper form a complete array a complete object so we are getting all of the data with the help of our form now let's work on storing the image and storing the data in our database so first of all let's create a new variable here and let's assign a null value for now and let's use if statement and this if statement in the service statement we will check if this image field has file or not so this function will basically return a true value or a false value and it depends on image field if this image field has a proper type of file then it will return true or else it will return false so in this if a statement we need to write the code that it will be responsible to store the image into our server so let's use the file name variable and the first thing we need to use is the request get scheme HTTP host and this function will basically return the HTTP host name of your application and after this let's concatenate the destination where you want to store your image so I want to store the image into the public folder and then assets and in this image folder so let's give it assets folder then image then slash and after this let's call the time function and after this let's concatenate a DOT value and then concatenate the extension of our file request image and then we can easily get the extension of image with the help of extension okay so this will return the complete destination that we need to store you know into our database and after this let's call the request image move function and this function will basically move our our image into the server so let's call public path and then let's give assets folder and then image folder because we want to store the image into our assets folder and image for under the image folder and after this let's use the request image so with the help of these two lines we are easily we can easily store the image into our application so let's comment this diagram and let's refresh the data and then I click continue I could see I would see the image into my public assets and image folder so let's continue this okay so into this as you can see we we have we got the file but in a temp form so we are not getting the proper file so this is because okay so this is because we should use here file name variable let's save this and let's go back again let's refresh this and let's use file data and let's choose an image and let's submit it again foreign with the help of form so let's delete this [Music] so we have successfully stored the image into our application now let's store this data into our database so let's use product equals to product model and let's use create function let's use first of all image and let's assign the image equals to file name and then name equals to request name and then the description equals to request description and the last one is the price that would also be coming from the request foreign we are storing the image into our server and with the help of this these lines we are storing the complete data into our database and then let's return redirect back to the previous page let's save this now let's go back again now let's choose an image and let's say product one and then let's give it anything because I'm just testing out the data the main thing we can easily store the image into our application let's submit this so let's go to the index page and here you can see we have our product data into our table and if I go to my localhost PHP my admin database I would see this data into my product table and this image is not showing because I need to show this image into proper image HTML form so let's use image and in this SRC we just need to add this image attribute that is coming from the image variable that is coming from the database let's save this let's refresh this and as you can see we are getting our image so let's give it a style width 50 person okay 20 percent and as you can see we are getting our data in a proper form and if I go to my database that is a laravel and if I go to the products table as you can see uh the data is coming into our database with the proper type of image so let's test this out again with uh with another image let's use this image and let's say product one product two and let's use dummy data okay so let's go back to the index page and as you can see we are easily getting the image so as you can see this is how we can store the image with the help of flowerville and you can easily get this code with the help of uh with the help of the description of this video and if you feel any issue you can easily comment me in the video or email me with your concern so whenever you deploy your application to the live server you do not need to change anything you just you will get the complete domain name with the help of this function and the same thing goes here so this line will work on both local and live server so I hope that this video will help you that how with how can you use uh with how you can store the image into your application with the help of larval if you have any issue let me know in the comment box below
Info
Channel: Takneeki Code
Views: 5,348
Rating: undefined out of 5
Keywords: laravel, laravel tutorial, image upload laravel, laravel image upload, laravel image upload with preview, laravel image upload in database, laravel image upload validation, laravel image upload and display, upload image in laravel, upload image in laravel 8
Id: Ejxkuh6JEXo
Channel Id: undefined
Length: 12min 47sec (767 seconds)
Published: Wed Feb 01 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.