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
16. Reviving product state

Episodes

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

Transcript

00:00
So our goal here is, based on the product that we've passed in, revive the state from this model, and then
00:06
allow us to see this in the form and, of course, edit it. So what we're going to do is head over to, like I said, create product. And the state is always going to be the same for this,
00:16
because, of course, it's a product that already exists in the database, and it has exactly the same properties. So we're going to go ahead and pull the state over to here
00:25
so it matches up with it. So the goal now is to revive the state of this. And to do this, we can use a mount hook within our Livewire component.
00:35
Now, what we're going to do is take the state and overwrite it with the array version of this model, this product model. So what we're going to do is we're
00:45
going to access this product. Really importantly, we're going to say without relations. So let's say without relations. We don't want any of the relationships for this included
00:55
in this array that we are giving out. And we're going to go ahead and use the toArray method. So that's going to hydrate the state with the current state of the product that we've passed in.
01:05
Now, if we head over to the browser and give this a refresh, sure enough, you can see all of the details here have been filled in. And you might have noticed that the price looks
01:13
a little bit funny, of course, because we are always on the UI showing this as dollars or pounds, which have a currency. In this case, it's been hydrated as cents,
01:23
so we're going to have to update that. And of course, we're not hydrating the files that are already attached to this product, so we're going to need to handle that as well.
01:31
So let's go ahead and tackle the price here because that's pretty important. And to do this, what we're going to do is explicitly set the price in here to a formatted version of the price
01:42
that we get through with an accessor. And remember, if we head over to our product model, we updated this earlier using Laravel's attribute functionality.
01:52
And we change this when we set the price, but we don't have any way to get a reliable price just yet. To make it super easy to work with money in Laravel, we're going to go ahead and use the Laravel money package,
02:05
which behind the scenes uses the PHP money package. This allows us to really easily deal with money in cents and convert it over to formatted versions, as well as add, subtract, divide, all that good stuff.
02:18
Now, we won't be doing much of that, but we will be formatting this, so this is really going to help us so we don't need to do any of that heavy lifting ourselves.
02:26
So this package is really easy to use. Of course, we're going to go over and require this in with Composer. And once that's done, we pretty much
02:33
have access to a money helper function, as well as functionality directly within Blade templates. So what we're going to do is just demonstrate how this works.
02:41
So let's go over to the home controller real quick, and I'll show you how this works. So we're going to go ahead and use that money function, and we're going to pass a cents amount into this.
02:51
Let's die dump on this just to see what we get back here. So I'm going to head over to our app, and we're going to go back over to the home page. And you can see here we get a money instance.
03:01
If we open this up in here, you can see we've got the amount, and it also gives us a default currency of USD. We'll have to change that, of course, if we change currencies, but we're going to be working with USD here.
03:13
So what we can do with this is we can format this. So we could, for example, format by decimal. And let's check out the result we get. And you can see we've got 999.
03:24
Or we can just go ahead and format this, and that's going to give us a dollar amount here with the currency symbol. So two really useful methods that we
03:34
can use throughout our app. But what we want to do is over on the price attribute here, we want to go ahead and when we get this back, like we kind of did with the setter,
03:46
instead what we want is this to be a money object. So we're going to go ahead and pass the price in there. And now any place that we access the price attribute on our model, we're going to get back a money instance,
03:58
which we can use to very easily format this. Now, the only problem we have with this at the moment is that we need to divide it by 100 to get back the correct amount.
04:07
So what we're going to do now is head over to our product page. And you can see that the difference here is that this gives us back an object now.
04:14
And what's actually happening is if we head over to edit product and we die dump on this state, what this is doing is because we are using the toArray method within Laravel,
04:24
that's converting that to an object with the amount with the currency and the formatted amount as well. Now, this doesn't give us the decimal amount.
04:32
So what we're going to have to do is in this specific instance, go ahead and specifically set the state price to a money object of that price.
04:42
So from here, we want to go ahead and grab the amount. So in here, we're going to say this state price and amount. That's going to go ahead and give us a money object. And now we can go ahead and say format by decimal.
04:58
So now that we've done that, if we head over to the product page, sure enough, we get 9.99 in there. There's probably another way that we can handle this,
05:05
but in this specific instance, we're not really displaying this. We're getting the value that we want and putting it into a form.
05:12
Okay, so that's pretty much it. We've now got a list of our products. When we click on this, we get all of the information to edit this product as well as the correct price.
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.