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
12. Validating and creating 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
Completely ignoring the files for just a moment, we're going to go ahead and now validate and store the product in the database via this form. Now, before we do that, I'm going to go ahead and get rid of the dummy products that we created earlier,
00:12
and I'm going to implement some validation rules for each of these and then go ahead and hit create product and have that saved to the user's product. So what we need to do is figure out the rules for each of these. Within Livewire, we can define these in a rules array within our component, and then we can just down here call a validate method. And that will validate everything for us. So let's head over to the rules and apply these now.
00:38
Now, we know that these are in a state object, so we need to say state dot title, much like we did when we applied these to our form. And we just give normal validation rules that we would within Laravel here. So, for example, required max 255 for the slug. Let's go ahead and say this is, of course, required.
00:56
We'll get rid of the max and let's keep the max, actually. And we're going to say that this is unique to the products table on the slug column. And then we've got our state description as well. So, again, we can just make sensible rules up for each of these.
01:11
Of course, you can tweak these if you wanted to. I'm going to get rid of the max in this case, just so we have a little bit more flexibility. We'll go ahead and validate the price as well. For this, of course, we're going to say it's required.
01:22
But we also want to make sure that this is a decimal. Now, we're storing it in cents, which we're going to do in the back end. But we want this to be a decimal with between zero and two decimal places. And we want this to be a minimum of one dollar in this case on the UI.
01:40
We're not covering making products free just yet because that requires a change via Stripe. But we will just keep the minimum at one for now. Now, for the live flag, we just want that to be a Boolean and we're pretty much done. Feel free to tweak these rules if you need to or if you find out you need to later.
01:57
Okay, so let's go ahead and see how this works. Just remember, over on our create product form, we have all of these X input errors, which pull from here. We haven't actually changed these over yet to state.title.
02:08
So, at the moment, what we'll find is when we try and submit this, nothing really happens. We don't see anything on the page. We switch these over to the actual names of the state and we hit this. You can see, sure enough, we get the validation messages in there,
02:21
which we can change and we'll look at that a little bit later if we need to tweak it around. So, for each of these, we just want to update these to say state.slug and state.description, state.price, just so we output these properly. For the live checkbox, we shouldn't have to actually add a validation message there at all.
02:40
Okay, so if I go ahead and click create product, there we go. We've got validation errors for each of these because this needs to be at least one and we should be good to start storing our products now. So, let's go ahead and over on the create product submit method, actually store this now.
02:56
So, to do this, we're going to go ahead and say auth user products, grab that relationship we set up earlier. We're going to create this and now is the great part because we already have this state within an array, we can just pass the entire state in.
03:13
We know the state's been validated, so we can just pass it directly in here and that will create the product for us. And then we can go ahead and return to a sensible page. So, we would probably in this case redirect over to the user's product page to show them that product.
03:29
Okay, let's try this out. We may see an error here because remember we are entering the price here as decimal over on the database. This is actually an integer. So, let's go ahead and just create our really basic product in here and set this to say 999,
03:45
hit live, click create product and yeah, sure enough, we see an error. So, as we would expect, we've got an invalid syntax type for an integer. Of course, we're using a float value. How are we going to get around this?
03:57
Well, what we're going to do is at the point anytime a product is stored, so if we head over to the product model to handle this, we're actually going to use a mutator to change the value of the price into cents by multiplying it by 100. To do this, there are a couple of ways, but we can actually use Laravel's attribute functionality to make this work.
04:20
Let's see how. So, we're going to go ahead and create a price method in here, which acts as an accessor as well as a mutator. So, when we retrieve this integer value out of the database, we want this to be in a money object format via a package, which we're going to pull in a little bit later.
04:37
We're not going to handle that now, we can cover that later, but we're going to go ahead and return in here this attribute object over from Eloquent. What this is going to allow us to do is make out an accessor and a getter. So, set is the mutator.
04:52
When we go ahead and create this, we're going to give this closure just in here, which is going to give us the price and remember that's a float value. And what that's going to do is whenever this gets set, we're going to change this by multiplying the price by 100. That's what we need to do.
05:09
So, basically, whenever price is set on this model, take the price and instead turn it into cents by multiplying it by 100. So, now the problem should be solved. When price gets set as we create that model, we shouldn't see that problem. Let's go ahead and just try again by creating our product in here and let's go ahead and set this to $999 again.
05:33
We'll set this to live and we'll create it. Okay, that didn't work, so let's just head over to our product again. We might need to say without object caching here to prevent any caching and let's go ahead and just make sure that explicitly returns that attribute.
05:49
So, we'll type in that as well and we'll change this to without object caching. Okay, let's try this again. So, let's head over and we'll say product, product description and let's set this to $999. Hit live and fingers crossed, there we go.
06:06
We have created the product. It is now listed over on our products page and if we just verify this in the database, you can see sure enough we've got $0.999 instead of $999 or whatever currency you're working in. We've got the product title, the slug, the description as well.
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.