In this video, learn the techniques to save uploaded files in a Laravel application.
- [Instructor] If you've followed along with this course, from the previous Laravel Essentials course, we're going to stick with that application and build on top of it. That application in Laravel Essentials built us a hotel booking application. We created that in this Laravel Essentials project. If you're coming to this course without having followed the one in part one, that's totally okay. You'll want to take a couple of steps, though, to update the exercise files to ensure that you can run this project. It may be beneficial for you to look into part one of this course for how we set up the homestead and the vagrant box for this project. If you prefer, you can take a couple steps here, and you may be able to figure it out on your own. In the homestead.yaml file from the exercise file, you'll need to update the map path on line 10. That updates the path for where your project is living. In this case, you can see that my code is living here in this users/justin/Documents/laravel-essentials directory. On your computer, it should be a different path. You'll need to update that path. You'll also, rather than running vagrant up, want to run the command vagrant reload. Reload is going to take your vagrant box and reload it and rebuild it. This should help work out any kinks on your system. After your vagrant box boots, you're going to want to run inside of the vagrant box, the command php artisan migrate colon fresh dash dash seed. This is going to run all the migrations for your vagrant box in our project, as well as seed the database with new records. You'll also want to run the command composer install again. Composer is going to install all the working files and related php projects we need for our application. This should solve the majority of the issues if you're coming without having followed along in part one of this course. We use homestead and vagrant to provide us with php, a web server, and a database server to operate our application. Then again, we can spin up using vagrant up from inside of our directory where our Laravel Essentials application lives. Once here, we can run the command vagrant ssh, and get in to our homestead virtual environment. From here, we can change directory to the code directory and we'll be able to run our php artisan commands. We'll start here with building out the ability to edit a room type. To be able to add a picture to our room types. To start, we'll need a new migration to add a field for room type picture field that we're going to add to our room types table. To do this, we'll create a new migration. We'll do this following the pattern that we saw earlier. And which your should know by now. It's php artisan make colon migration and then the name of the migration. In this case, I'll name the migration AddRoomTypePictures. We can open up that migration by viewing our code base in our text editor and going into the database migrations and clicking on the migration file for this. On line 16 is our up function, which as we saw before, is where we run our migrations going forward. In this case, we'll replace line 16 with schema colon colon table room underscore types coma function, and the parameter to the function will be blueprint and the variable table. We can then add our field to this table with table arrow long text pass in the string for the name of the field being picture, arrow comment to add a comment for this field. In this case, we'll just add the picture filepath, and on line 18, we'll add after in parenthesis and then the string to the after function will be the field that we want this field to come after, in this case, description, arrow, nullable. In our down function, we want to enable this to be able to drop the field in question. In this case, I'm going to copy lines 16 through lines 19, paste it on lines 29 through 32. I'm going to delete all of line 30 and 31 except for the table arrow. And here, we'll just add table arrow drop column in parenthesis and then the string with the field name that we want to drop, in this case, picture. Now we can go back to our console and run the migration, php artisan migrate. We also need to ensure our storage directories are set up. The storage directories in Laravel are where Laravel will be able to store our files. We'll do this with running the command php artisan storage colon link. Now we can just build out an interface to edit our room types, including the ability to save a file. First, we're going to need a controller. We'll do this like we've done almost everything else in Laravel, with running a php artisan make command. In this case, the command will be php artisan make colon controller RoomTypeController dash dash resource, to make it a resource controller, and then dash dash model equals room type to define the model that should be associated with this controller. Next, we'll update the routes to support our room type controller. To do that, we'll go back to our text editor and open up the file in our routes directory for web.php. We'll copy line 26 for our bookings controller, and paste it in as line 28, and rename the resource route as room underscore types, and rather than booking controller, it'll be room type controller. Now we just need to add the room edit view file and update the controller and verify we can set an image for the page. Our edit page, we'll grab from the resources for this lesson. In the exercise files, for this lesson, there should be an edit.blade.php file. If you open that file up, copy all of it, and go back to our text editor, we're going to add this file as resources, views. We'll create a new view folder called room types, in here we'll create a new file that will be edit.blade.php. And we'll paste in the contents from the exercise file. Now, to set up our controller, we're going to open up the controller at app, http, controllers, room type controller.php. We'll scroll down to our edit function, on line 58, and we'll replace line 60 with return view parenthesis pass in a string for the view file we're going to return. In this case, roomTypes.edit. And then arrow with our room type. We're also going to go ahead and set up our controller to enable us to save our room type with the picture. This is going to be very short. On line 72, all we need to do is roomType arrow picture is going to be equal to request, so we're talking to the request, arrow file, this is a special function on the request object, that returns any files that was passed in. In this case, we then pass in the name of the file with picture, and then arrow, store, and then string public. This is a storage location for our file. On line 73, all we need to do is roomType arrow save, and then line 74 we'll just redirect with return redirect arrow action. And we'll go to [email protected] And now we'll go to our browser. And in our browser, we'll go to http/homestead.test/roomtypes/1/edit. And we'll select a picture to save and upload for our room type. All we have at this point is that we successfully redirected. In the next video, we'll start showing these files off.
- Saving and viewing files
- Displaying validation errors
- Console commands, outputs, arguments, and inputs
- Building a Laravel route that has multiple parameters
- Working with the Laravel authentication system
- Preparing for deployment