تماشای این درس نیاز به اشتراک حرفه‌ای دارد.

Installing Money Package0:00

Right now, we only get a simple integer whenever we request a price. Let's make sure that we have an object to work with so we can format the price and to make it easier to do calculations in the future as well. Let's head over to Packages. If you search for money, you can see that there is a PHP money package which has been downloaded quite a few times, and that's the one that we'll currently use in this project. Let's composer require the package from Packages. There we go. Now we can head over to our product,

Creating Price Accessor0:40

There we go. Now we can head over to our product, and we can implement an accessor for our price. Let's define a new method, and let's call it price. That's going to return the attribute. Now we can say return attribute make, and there's a getter and a setter. In this case, we say get. We can use a closure.

In this case, we say get. We can use a closure. Whenever we get the price from the database, which is an integer, we want to transform it into a Money object. The value is going to be equal to the price from the database. We're going to return a new Money object, so we're going to say return new Money(). The first argument is going to be the amount, which is going to be equal to the value from the database. Next, we've got the currency, and let's set it to USD.

which is going to be equal to the value from the database. Next, we've got the currency, and let's set it to USD. Now when we switch back to the browser, we'll probably get an error because we're trying to echo out the price, which is now an object. There we go. If you refresh the page, you can see that it cannot show the value because a string is expected, but money is given. Now there's a really cool way in Laravel to ensure that whenever we try to echo out a money value in Blade, we can automatically make sure that it is formatted correctly.

Auto-Formatting in Blade2:04

that whenever we try to echo out a money value in Blade, we can automatically make sure that it is formatted correctly. Let's set this up as well. Let's head back to our code, open our AppServiceProvider, and in our boot method, we're going to reference our Blade facade, and we're going to say stringable. The stringable function that we're calling on our Blade facade is accepting a callback. Depending on what you pass to this callback,

is accepting a callback. Depending on what you pass to this callback, Blade will try to recognize it and return the callback output rather than the object itself, if that makes sense. In this case, we're instructing Blade. Whenever you receive a money object, I want you to return this instead. Now we can reference the money object and call the specific formatting functions. Let's do that right now.

Configuring Money Formatter2:54

and call the specific formatting functions. Let's do that right now. If you want to know more about the Money class, you can look at the documentation. Now I'm just going to reference some code that they have listed in order to format your money using an international currency. We've got our currencies. Next, we're going to set up our NumberFormatter that is going to be set up to format a number which corresponds to how it should be formatted in the US.

that is going to be set up to format a number which corresponds to how it should be formatted in the US. Finally, we'll set up our moneyFormatter and we'll provide this with our numberFormatter and all of our currencies. Finally, we're going to return the formatted value. We're going to reference our moneyFormatter. We call the format method, and we're going to pass it the money object. Now, when we switch back to the browser, you can see that our prices are now neatly formatted.

Styling Product Cards3:38

Now, when we switch back to the browser, you can see that our prices are now neatly formatted. This is pretty cool because now whenever we play a money object and we return it in our blade file, it is automatically formatted on the fly. Let's add a little bit of extra styling. Let's switch back to our storefront and let's give our product a background. We'll give it some rounded corners, some shadow,

We'll give it some rounded corners, some shadow, a little bit of padding, and let's take a quick look. Okay, that looks pretty good. I think we can wrap up this video and move on to the next one.

دوست دارید گاهی خبرهای Laracasts را ایمیل کنیم؟