Playing
09. Set a Default Currency for the Number Helper

Transcript

00:00
Laravel's Number Helper gives us a bunch of functionality, but one of them is the ability to format currency. This means that we can take a standard value like this and format it into something that actually looks like currency alongside a currency symbol.
00:15
Let's go ahead and try this out and then we'll look at the newest feature of this where we can globally set a currency, which makes our life a lot easier if we're just dealing with single currencies or don't want to have to keep specifying these. So to use the number helper, we just pull in the illuminate support number class and then the currency functionality. We just use currency. We give a value in here.
00:37
By default, this is set to USD. So if we head over to the browser here, you can see that this is formatted with dollars. However, we can go ahead and pass as a second argument in here any other currency code, and that will go ahead and change the currency symbol over for us and the formatting of the amount depending on the currency. Now, this is pretty annoying because we don't always want to have to specify in here.
01:00
Not everyone works with a default of USD. OK, so let's go ahead and just comment this out, pull this down and we'll get rid of GBV here. Obviously, we already know that's going to go ahead and go back to the default. So how do we set the default? Well, we can use the number class here and we don't use default currency that just gives us what the default currency is.
01:20
Instead, we want to use use currency. So in here we can just pass in the currency code of the currency we want to work with. In my case, let's change this to British pounds and let's go over and give that a refresh. So any call to number currency after you have set use currency is going to use the currency that you want.
01:40
So you could either set this manually or you could set it based on a database value. For example, if you had a user and you had a currency set for this or you had some sort of payment plan and you wanted to set the currency from a database value, you could do that as well. Now, probably the most convenient way to store this unless you are changing over the currency on a per request basis,
02:03
like within a controller, is to put this in your app service provider. So we can put this here, but let's just comment this out for now and head over to our app service provider. We'll go down to our boot method and we'll go ahead and just pop this directly in here, making sure we pull the namespace in. So globally setting something like this would be absolutely fine.
02:22
Of course, what you could also do is still pull this from a user if you wanted to. For example, if the currency was set on your user, this would be great for multi tenant applications as well, where each of your tenants had a certain currency associated with them. But we're just going to go ahead and hard code this in there for now.
02:39
OK, let's see if it works. And yeah, sure enough, it does. That is now globally set within our service provider and used throughout our application. So there we go. A really great way to go ahead and set the default currency. So we don't have to specify every single time we make a call to number and currency.
26 episodes2 hrs 34 mins

Overview

Need to know what’s new in Laravel as it happens? Every episode of this course is dedicated to covering the most interesting and useful Laravel additions in detail, so you’re ready to start using them in your applications.

Check back often, and stay completely up-to-date with Laravel.

Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!

Episode discussion

No comments, yet. Be the first!