This episode is for members only

Sign up to access "Build a Livewire Drag & Drop Uploader" right now.

Get started
Already a member? Sign in to continue
06. Storing files


We've got a bunch of these temporary uploaded files, but we want to move them and store them somewhere.
It's always a good idea to also store these in your database so you have a nice, easy reference to them that links the file path that you've moved them to. So the first thing that we're going to do
is go ahead and create out a file model and, of course, a database table. So let's make out a file model and create a migration alongside of that as well.
So we're going to go ahead and open up the Create Files Table Migration, and we'll just try that again. There we go. And if we head over to here,
we don't really need to store too much about this. We probably want to store the original file name so we can identify it. So file name is good.
And we want to store the path that it's stored either on our local file system or a remote file system like Amazon S3. So let's go and migrate these changes,
phpArtisan Migrate, and we're done. So what we can now do is over in this component here, we've got a list of these files, which we can access either here
or directly from the property, and we can go ahead and iterate over them and create a file record in the database for these, and at the same time,
upload them to a path in our file system. So we're going to go ahead and, because this is an array, collect these up, the Laravel collection.
That means that we can easily then just use each to iterate over them. You can use a for each here if you want to. And for each of these, we have a file.
So that's now iterating through each of the files that we have uploaded. And now we can go ahead and create our file model for each of these.
So what kind of things do we want in here? Well, we want the file name, and let's just pull in a model really quickly, app models file.
And the name of this, and we're going to assign it here at the same time so we can use it again in a moment, is the file.
And remember, this is a temporary uploaded file, which we saw dumped earlier. That contains a method in here called get client original name.
So that's the name of the file that we uploaded. So for example, video.mp4. So next is the file path, which at the same time,
as assigning this to the record that we're creating in the database, we can also store this file. So if we again open up the temporary uploaded file,
we have this store as method where we can add in the path, the name of this, and then any options.
So for here, we're going to go ahead and say file, store as, we're going to store it in this files directory, which don't confuse with the name as our property,
and then we're going to pass in the name. So that's the name of the file that we want to call it as. So we're basically putting this file into a files directory on our local file system,
and we are calling it by the name that we've assigned here, which is the original name of the file. So you could get conflicts here technically. You may want to just sort of add on
maybe an MD5 hash of the file name if you want to. It's entirely up to you how you name these. Just do something a little bit different. Okay, now that should be it.
So if we head over to our database, we've now got this files database table. Let's just try this with one file and just see what happens.
So we'll just upload this introduction file. And yeah, sure enough, over on that file model, if we just open this up,
we can either unguard this or we can go ahead and set the fillable properties in here to file name and file path, like so. Okay, let's try that again.
Hit upload a file, hit that, and we should be good. So nothing has happened. We're not seeing anything happen at the moment,
but what should have happened is this has been created in the database with the original file name that we gave, and we've got the file path just here.
So let's figure out where this has been stored first of all. So if we just head over to our storage here under app, right next to Livewire temp, we've got this files folder,
which is if we just head back over to our uploader, the name that we gave here, and we've got the file in there. So that's been uploaded and we can now technically,
once we've symbolically linked our storage directory to public, we can download them, which we're gonna look at in a couple of parts time.
So this is gonna work with multiple files as well, whether we drag and drop, it doesn't really matter. So let's just choose a video four and five here. And if we head over, there we go,
four and five have been uploaded. So really straightforward. They're now uploaded. They're stored in the database so we can list them
and show the user which files have been uploaded. And of course we have a reference over in the database to the location of these so we can re-download them when we need.
9 episodes 43 mins


Build a multiple file drag-and-drop uploader with Livewire and Alpine.js.

Using Alpine and Livewire’s JavaScript API, we’ll directly hook into the JavaScript drop event and kick off the upload progress, validating files and showing a progress bar along the way.

So, if you need drag-and-drop uploading in your Laravel project, this course has you covered!

Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!


No comments, yet. Be the first to leave a comment.