This episode is for members only

Sign up to access "Build a Livewire CSV Importer/Exporter" right now.

Get started
Already a member? Sign in to continue
Playing
05. Casting money

Transcript

00:00
When we are importing and exporting data we need to be mindful of how we're formatting certain columns. Now we're using the amount as the kind of outlier example here. So what we're going to do now is add in a cost to this. So within our application we can actually see this as a dollar amount but then when we export it
00:19
we want to see this as a standardized amount and then when we import it we need to take a value like three dollars fifty and then transform this into an integer. So the reason I've left this as an additional episode is so we can focus on the cost of this and how it will interact with what we're doing a little bit later.
00:41
So how do we transform this value? There are a ton of different ways that we can do this but I always reach for the Laravel money package. So let's go ahead and install this and get this cost.
00:53
So if we head over to our application and pull this package in. Once that's done we're going to go ahead and head over to the cost section here which will give us a bunch of costs that we can use. Now we know that we are using integers within our database
01:06
so if we take this what we can do is over in our transaction here we can add to our cost. Now I added this date cost a little bit later we don't necessarily need this but the amount can now become a money integer cost and this class will handle taking the value in the database like the cents amount
01:27
and it will basically convert it over to something like this when we dump it out. And then if we import with this value like ten dollars that will be converted back over to an integer for us so we'll keep an eye on that a little bit later. For now though let's go back over to our application give this a refresh and there we go.
01:44
So we now have a nice formatted amount. Let's keep this in mind for when we're going through the process of exporting and importing and we'll keep an eye on how these values look as we generate our csv and as we take a csv and pull that back into our database.

Episode summary

In this episode, we take a closer look at how to manage money values when importing and exporting data in our application. Money columns can be tricky since they need to be both human-readable (like "$3.50") and consistently stored in the database (as integers, such as cents).

You'll see how we introduce a new 'cost' field and use the popular Laravel money package to help with formatting and casting these values. This package makes it super easy to handle conversions both ways: from a formatted dollar amount to an integer for storage, and back again when displaying or exporting data.

We walk through pulling in the package, updating our database interaction so amounts are handled as money values, and test it out to see how the app correctly shows formatted currency. Finally, we talk about the importance of keeping this formatting consistent when we export to or import from a CSV file—so our data stays reliable and looks good in every context.

Episode discussion

No comments, yet. Be the first!