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
18. Pulling together the product update process

Episodes

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

Transcript

00:00
OK, so let's work on submitting this form to actually update the product and also modify the files as well. Now, at the moment, the submit method on our edit product component doesn't exist.
00:11
So I'm just going to come up to the top here just under mount and go ahead and create out a submit method like we have before. Now, we want to be really careful here because, of course, we want to validate this as well. So over on create product, we're going to go ahead and grab the same validation rules that we created before. And we're going to go ahead and put them just at the top here.
00:32
So the first thing that we want to do when we edit a product is, of course, validate this. Now, we're going to see an issue here, because if we go ahead and save this out, the slug technically already exists in the database. So if I edit this, you can see the state slug has already been taken. So we can't actually save this out because the slug that we're already using, of course, already exists in the database.
00:55
Now, to get around this with Laravel validation rules, we can add a third argument to this rule, and that's going to be the ID of any of the things to ignore. Now, we can't quite do this because we're working with the property here. Ideally, what we'd do is append on this product ID. But because we're working within a property here, that's just not valid PHP.
01:17
So what we need to do is change this rules property over to a method. So I'm going to go ahead and grab the array itself here, and I'm going to go ahead and create out a function in here called rules, a method in here called rules, and I'm just going to return that array. That means that these rules will still be read, but because we're working in the context of a method, we can reference the product ID. So if we come back over and we hit edit product, sure enough, that validation is passed.
01:45
So we've got validation working. Now, we need to go ahead and update the product itself. That's going to be really easy. We're going to access the product model.
01:54
We're going to hit update on that, and we're going to pass through the current state that we have. So if we head over now and just say product updated, what we're going to do is also update the slug and the description. Let's change the price as well to $15.99, and we'll hit edit, and that should edit it. We are going to be implementing a little notification here to show that it has been successfully saved.
02:19
But for now, we can just refresh the page and you can see everything has changed nicely. Okay, so onto files. This might seem like it's more complicated than it will actually be, but it's not too difficult. The first thing that we want to do is over on the create product, do exactly the same thing that we did here.
02:37
We want to go ahead and save all of the files, the new files that have been updated. So a little bit of duplication here, but I don't think that's too much worry since we're working in two very small components. So I've gone ahead and pulled in the file model, and we're changing this reference because we're not creating a product over to this product here. So now technically what's going to happen is we're going to see any new files roll in for any new files we add.
03:06
So let's add this package.json file and hit edit product. And if we come over to the database, sure enough, you can see that's been added in. Let's now look at removing existing files from this product. If I want to remove this markdown file, that needs to be detached from the product file relationship.
03:27
So to do this, what we're going to do is say this product and files. And we want to say where in because we want to pick out the specific files that are in the list of deleted files or removed files. And we know that's within removed files. And then we're just going to go ahead and delete them files out of there.
03:48
So save any new files that have been uploaded and go ahead and find the files that we've chosen to remove existing files and just delete them. That's all we need to do. So let's go over and just try this out. I'm going to go ahead and at the same time as removing readme, I'm going to add in the composer.json file.
04:08
So let's go ahead and click edit. And there we go. So if we head over to the database, readme should have gone and composer.json is in there. And of course, this is reflected here in the current list of files.

Episode summary

In this episode, we pull everything together to make our product update process work smoothly! We start by creating a submit method on our edit product component (since it didn't exist yet) and bring over the validation rules from the create product component. But there's a snag: when editing, the product's slug already exists, so our validation would fail. We fix this by changing the validation rules from a property to a method, letting us specify which product to ignore in the uniqueness check.

With validation working, we update the product details using the model's update method. We test it out — change the price, description, or slug — and see the updates reflected right away. Next, we tackle handling files. We handle adding new files much like on product creation, and then handle removing files by detaching them from the product using a simple query based on the files we've marked for removal.

Finally, we try everything out: remove a file, add a new one, update the product, and check the database to see all changes reflected. That's all there is to getting both product details and files updated together in the edit process!

Episode discussion

No comments, yet. Be the first!