Playing
03. Creating and seeding transactions

Transcript

00:00
Obviously the data that you are going to be importing and exporting is going to vary massively from what I'm doing here but let's go ahead and create out a model with a couple of different data types
00:11
and the reason I want to do this is because for things like amounts, so dollar amounts with cents and dollars or whatever currency you're using, we want to store this as an integer and cast it so we're not just looking at very basic data types like basic integers and strings so let's go ahead and create out this model, we'll get all this data seeded really importantly
00:34
so we can actually test exporting it which is what we're going to be doing first and then we're good to go okay so we are going to go ahead and make out a model here, say make model and we are going to call this transaction, we'll create a migration factory alongside of that as well and let's open up the create transaction table and just fill in a few things that we might want to store
00:57
so the first thing is going to be a string with the transaction IDs, imagine these were sort of coming from a bank or something like this, now really importantly we're going to have at least one column in here which is going to be unique, we're going to have the ID in here but we're not actually going to export the ID, we're going to be exporting the transaction ID that becomes our kind of
01:20
primary key or unique column, we're not actually going to make it a primary key next up we can just add any kind of basic data so things like a descriptor for a payment that's just going to be a basic string, next we want the transaction amount which is the kind of thing I'm getting at here, we want an integer for the amount so for example if we were to just store
01:42
one dollar that would be stored as 100 cents, this is the recommended and the only way I would ever store money values so we'll look at how we cast that very shortly, let's go ahead and also add a timestamp here or a date time and that is going to be the date of the transaction so we have our normal created and updated at but we also have this additional date which is when the
02:06
transaction happened, so I think that is enough data for now let's go and create this out so let's say php artisan migrate, get that table created out in our database and we should be good, okay so next up let's go over to our transaction factory so we can fill in some of the columns here that we want to fake out, we're going to need to generate quite a few fake records so we can test
02:32
exporting these even up to the hundreds of thousands or millions, so the first one is going to be the transaction id, how would we fake this? well we could do something like this fake unique and then we can actually use the shuffle method which allows us to pass in a bunch of string characters and this will just randomly generate from this list of characters a code for us or a
02:56
transaction id for us so you can go ahead and add that but I'll keep it like that for now, for the date of the transaction this is pretty straightforward we can just say fake date and then we want to provide a earliest date possible so let's just say we want this to be within 10 years and we'll use the now with sub years to do that, next up we're going to say amount so let's
03:19
say fake random number and this is the amount of digits not the actual max integer so this will be any five digit number so it could be something like this which would be $567.89, next up the descriptor this can again be pretty straightforward so descriptor can just be a fake sentence with four words in there, okay great let's go ahead and seed out a bunch of these and we are going to go
03:51
and seed out a lot so let's say php artisan tinker or we could create a specific seeder file for this but let's just do this on the command line let's say app models transaction let's grab the factory for that and let's generate our 100,000 we don't want to do too many because when we come around to exporting and queuing huge imports or exports like this it's actually going
04:18
to take too long so we won't be able to sit and wait for this to finish so let's just do 100,000 for now but we know that this is going to be capable up to potentially millions, okay so we'll just wait for this to generate and then we'll check our database out and that looks like it's done so let's go ahead and just check the database over here and it looks like yeah we've got quite
04:40
a few in here let's do a count on the id from transactions just to see the total here and yeah great sure enough we've got 100,000 so we've got plenty of data here which we can use to export and then once we've exported it we're going to have the right structure csv that we can then use to re-import this data back in
22 episodes2 hrs 18 mins

Overview

Let's build a powerful CSV importer and exporter with Livewire, completely from scratch.

This can handle millions of rows, be reused for multiple models, and by using queues, doesn't require the browser to be open.

This course is for you if:

  • You need a robust drop-in importer and exporter component for your models that you have full control over
  • You want to brush up on some advanced Livewire concepts
  • You want to learn about queues in Laravel
Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!

Episode discussion

No comments, yet. Be the first!