PHP Laravel Tutorial for Beginners 17 - Laravel File Upload Example

 

hey what's up guys welcome back to our blog and welcome to this blog so this is part 17 of our laravel for beginners tutorial and as a sort of review in the last blog we have added the access control for the post or blog post available in our project and we also limit the edit and delete buttons for those owners of the actual post so in this blog we're going to add a file upload so in here so for example if we add post so in here what we want is to add a file upload where we can actually upload an image so we will be able to do that and let's try to go back to our code editor and open up our create that laid that PHP and we'll try to add the file upload capability in our project so under resources and then use post and then create that blade that PHP so we have here deform that enables us to add a post so we're just going to add here the element for our file upload so in here we need to add the live with class form group and of course we need to add the actual form element which is we're going to use the laravel collective's for that so form and then file and then we're just going to name it as a cover image okay so in order for us to submit or upload the file in this particular form we need to change also the ink type for this form in this particular part we need to add another which is the ink type for our form and then it should say multi-part multi-part /data and then save let's try to preview that refresh and then you can see here the file upload element where we can browse the actual file in our file system but if you try to select an image here and then submit it's not it's not going anywhere for now it's not saving to our database and it's not saving into our project because this is only the user interface and there's nothing logic or there's no logic behind this file element so in order for us to have the actual logic we need to go back to our auditor code editor and open up the controller so HTTP controllers and then inside our post controller but before we forgot I think we need to add first the attribute or the field into our database let's open up our database and try to add the field first by using our migration so I'm just going to show you here so inter post structure and we don't have yet the field for our image or cover image so we need to add that by using our migration so open up the terminal first and here we need to run the command PHP HP artisan and then make migration and then the name of this migration is odd cover image to the post okay okay so the migration has been created so let's open up it here in our database and then migrations so odd cover image to post and we need to add logic here to actually add the cover image attribute so table and then string this will just add the cover image into our post table and of course in here we also have the down functions okay so we're just going to drop column in here so drop column and then we're just going to say cover image and after that we need to run the migration so PHP artisan and then migrate so migrated if you try to go back to our page me my admin and then refresh our structure so we can see here the cover image field or attribute so now let's go add the logic to actually upload the file into our file system and also save the file name into our database so in our file upload as I've said there are two things that will happen so we need to add the string path or a dealer the name of the file in here in our cover image attribute or field and then we also need to upload the actual file into our project so now let's go back to our post controller open up our post controller and and here we need to modify let's just go close the terminal for now [Music] let's just close this and then we need to modify the store function so we need to add the validation for our cover image first so the cover image should be image and then it is nullable meaning the user can have the option to upload or not upload a file for their cover image and also we're going to set the maximum size of the file in to 1999 because most common page settings they're allowing only 2 MB of file upload so we're going to set the maximum size of our image into 1999 which is almost 2 MB or 2 megabytes and then in here we need to set up few things so we're going to handle the under file upload and the file upload and in here we have to say request and then pass file we're just going to check if the user has opted to upload the file so else we need to set the file name file name to store yes/no image that JPEG so this isn't going to be the default image for our user if he has opted to not upload cover image for for himself so this will be the default so if the user has opted to submit or upload the file so it should say here we need to get fire we need to get the file name with the extension in here so get file name with extension get file in with extensions okay and then it should say file name with ext is constant request and then the file which has the name of cover image and we're just going to get client original original image okay and then we're just going to get just the file name of the file in order for us to do that so file name and then path info and then we're going to pass in parameters in here finally name with extension and we're just going to get the part info underscore file name so I think this blog is further along so we will continue working on with this file upload in the next blog so see you in the next blog thanks 

Post a Comment

Previous Post Next Post

Recent in Technology News