This episode is for members only

Sign up to access "Build a File Marketplace with Laravel" right now.

Get started
Already a member? Sign in to continue
Playing
14. Attaching files to a product

Episodes

0%
Your progress
  • Total: 3h 32m
  • Played: 0m
  • Remaining: 3h 32m
Join or sign in to track your progress

Transcript

00:00
So right about here, just after we've created the product, we want to go ahead and actually attach the files to the product. We've already seen that the product creation does work, which I'm actually going to delete here. But of course, we're going to need a new table for this so we can store these files in. So let's start with that. We'll create our model for this. So again,
00:19
we'll use Artisan to make out a model, call that file and create a migration. Let's head straight over to the create files table migration. And let's go ahead and fill in what we need to store here. Now each of the files belongs to a product this time, not to the user itself. So we're going to add in a foreign ID with the product ID. We'll go ahead and add a database constraint here. And
00:43
we'll say that on delete, we want to go ahead and cascade. So if the product gets deleted, the files also get deleted as well. You might want to handle that really sensitively in the future, because if a user does have the ability to delete a product, of course, you still want users to access the files. But for now, this is just the safest option. So what other information do we
01:03
want to store about each of the files? Well, we're going to go ahead and store a file name, which will be the actual file name like readme.md. In this case, we've been using as an example. So that's going to be the sort of file name that people can see and understand. And then we're going to store the path to the file on our file system once it has been uploaded. And that's
01:24
pretty much all we need. So we can go ahead and migrate this now. And let's just set up the relationships really quickly. So over on the product model, just down the bottom here, let's go ahead and create a relationship for the files for this particular product. And once again, really simple relationship. It's just going to have many files like so. Okay, so now that we've
01:44
done that, what we can actually do is get to the point where we attach the files after we have created the product. So we're going to go ahead and assign the product that we get back from this creation to a variable so we can start to attach this properly. And now for each of the files that we've added to this array here that we're iterating over, we effectively want to collect these up,
02:07
convert them over to a file model, and then save them all in one go. What we don't want to do is iterate through each of the files and perform a database query for each of them. So to do this, what we're going to do is assign this first of all, we're going to use Laravel's collect function, which will put these files into a collection. We're then going to go ahead and map over these
02:32
to create a new thing within this collection. So we can go ahead and create out a closure in here, and for each of these we'll get the file. And what we want to return from this is a new file. So we want to use the file model that we've just created, and we want to say make. Now we want to say make because we don't want this to persist at the database level just yet until we pass all of
02:54
these through to the product relationship to create the files. Now if what we're doing here doesn't make too much sense at the moment, don't worry, I'm going to die and dump on these files so we can actually see what is happening. But let's go ahead and just pass in the data that we would expect when we build up a file. So before we do that, let's head over to the file model and just add our fillable
03:13
columns in here. So our fillable columns, as we know, are the file name and the path. So that's all we need in there. Okay, so the file name we've already actually seen, it's just file get original client name. So that's going to be the file that the user sees. And then the path is going to be where this is stored. Now at the moment, when we are adding these files within Livewire, they're
03:39
stored in a temporary directory. They're not actually persisted anywhere in our file system within Laravel. But what we can do is store this at the same time as actually grabbing the path back. So to store a file within Livewire, we take the temporary uploaded file and we call the store method on this. And we choose the directory that this is going to go in. Now bear in mind that this
04:01
will go in the default public directory within Laravel. Public doesn't mean it's accessible by the public, but it will just go into that and we can later download it. So that is all we need to do. Now let's go ahead and just die dump here on the files that we've collected up. We're then going to get rid of the validation and the product creation, just so we can focus on seeing what
04:23
is returned by this. And we'll also keep an eye on our storage area. If we just close this off and we'll see the files in here. Now at the moment, you can see we've got this Livewire temp directory. This contains all of them temporary files that we've already been uploading. But we'll see them put into the public directory once we're done here in a files folder. Okay, so let's give this a
04:47
refresh. Go over and just choose a couple of files and let's see what we get here. So let's click create product and yes, I hope this doesn't exist. Let's just have a look here. I've put this in the wrong order, so it's actually get client original name. That would help if we call the right method and let's come over and just try that one more time. Okay, so I'm going to pick readme and let's
05:10
choose this Tailwind config and there we go. So this collection that we have built up and mapped through now contains a Laravel file model with the information about each of the files. This has now actually been stored within our file system. So if we come over to files here, it's not actually been put in public. These are the two files that we have just uploaded using Livewire. So that's
05:34
actually been uploaded now. So now that we've done that and we know that these files are actually being uploaded and created to a collection of file models, we can take the relationship on the product to the files and store all of these files in one go. So really we're only performing one query to create the product and then down here we're going to create one query to store all of
05:56
the files. To do that we access the files relationship on the product and we can use the save many method and we can pass that collection of files in. That's all we need to do. They will be saved for that product that we create and each of the files will be created in the database. So let's try it out. So we're going to head over here, give this a refresh and we'll just say product.
06:20
Keep this really simple for now and let's set a price in here. Set this to live. We're going to choose a couple of files. We're going to hit create product and there we go. So now if we head over to our database we've got the product, the files have been stored and of course as we've already seen the files now exist on our file system.
34 episodes3 hrs 32 mins

Overview

Build a marketplace where sellers can list and sell files, while we take a cut of each sale using Stripe Connect.

We'll cover onboarding users with Stripe Connect, creating products and uploading files, payments, and delivering purchased files to your customers.

Here's everything we'll cover:

  • The Stripe Connect onboarding flow
  • Effortlessly creating products (and uploading files) with Livewire forms
  • Subdomains for your user's marketplace
  • Stripe Checkout for a beautiful, secure payment flow
  • Securely delivering files with Signed URLs in Laravel
  • Showing sales stats on a dashboard
Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!

Comments

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