This episode is for members only

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

Get started
Already a member? Sign in to continue
Playing
03. Customer model, seeding and exporting

Transcript

00:00
Before we start to even attempt uploading a CSV file we're going to need some data and we may as well create out the customers table here and include a factory, generate some records and export the CSV so we've got it ready to upload to re-import to our database. So the first thing
00:18
that we're going to do is create out a model in here, it doesn't matter what you do here, the concept remains the same, but I'm going to create a customer model with a migration and really really importantly a factory so we can generate that out. So once that's done we're going to head straight over to the create customers table migration and let's just
00:36
fill in pretty much as much data as we think we're going to need here. So I'm going to go ahead and add a string here for the first name, remember some of these can be optional, so for example the last name might be optional in which case you would flag that as nullable, we're going to leave the first name and last name required just for now. If we come down here we're going
00:54
to add in an email that's going to need to be unique, really important when we're importing because we don't want to cause any errors and override these and we'll check how that handles a little bit later. We're also going to have a company as well, we're going to make that nullable so let's just add a string for a company, make that nullable and we're also going to have a
01:15
VIP state in here so that's going to be a boolean so we're going to check how that works nicely and that is going to be nullable because we might want to set that to null, in fact it's probably a better idea to say default for this and set that to zero or false and lastly let's just finish up the date. So we really just want a few different data types in
01:38
here so we're going to add in the customer's birthday in here and we're going to make that nullable just in case it's not being provided. So I think this is okay, you can obviously play around with some other data and we're also going to be looking at these timestamps and how they work for importing as well but I think that's enough data for now. So we can go ahead and
01:56
migrate this so let's run php artisan migrate and we're good to go. Let's head over to the database and there is our table. So now we need to just generate a load of records, we're not going to go wild and generate sort of a million right now because as we're developing this it's going to be a little bit slower so let's just go for a sensible amount, we'll maybe pick 100 or something like
02:16
that. So if we head over to the customer factory under database and factories what we can do here if you've not worked with factories before is just provide a definition for the fake data that we want to generate out. So in our case we want to go ahead and use a first name, so let's go ahead and say this faker which accesses the php faker instance and we're just going to call the first name method
02:40
in here to generate out a random fake first name and we'll do the same for last name and of course we'll switch this method up to last name email is just as easy we can just go ahead and swap that and that over here now with this one when we're generating fake data it's really important that we chain on unique because if we generate two email addresses that are exactly the same while
03:02
we're seeding this data we're going to see an error so we want this to keep track of what was already in processed so we can get some unique records in there. So same with the company we can get rid of unique for this because it doesn't really matter too much and we'll go ahead and call the company method with the vip state let's just set this to false we don't really care about
03:21
that too much for now and the birthday here let's go ahead and fill this in this is slightly different we want to say date time this century so we want something a little bit more accurate that will just give us a date of birth for this century which is more likely to be a birthday okay so now that we fill this in there's a couple of ways to do this you can either create a seeder
03:44
you can come over to tinker well if that's a piece of software that you use but i'm just going to go ahead and do this on the command line so we're going to run php artisan tinker here and then we can just start generating out some records so let's go ahead and say customer factory and we can choose how many times we want to do this so let's just do 100 for now
04:02
and then let's chain on create that's going to persist these in the database so if we run this you can see sure enough we now have 100 fake records in here so now that we've got these records we need to export them obviously depending on the database software you're using you may or may not be able to do this you probably should be able to do this but i'm going to go ahead and do
04:22
this from here which is postico so we're going to head over to customers here and we're going to say export customers i have a folder here on my desktop called live wire import csv which we can store any of our csv files in and that's pretty much it that's all we really need to do so we're just going to go ahead and save this out and that will now have given us a csv which we can use to import
04:45
back into our database table so eventually we're going to go ahead and truncate all of these and then just see this fill up with exactly the same data as we've seen from the introduction so there we go we are pretty prepared now with our data and we can start working on the csv importer component

Episode summary

In this episode, we're getting all set up with some sample data so we can play around with CSV importing later on. First, we create a new Customer model, set up the migration for the customers table, and make sure to include all the fields we might need (like first name, last name, email, company, VIP status, and birthday). We pay special attention to things like making emails unique (since that's important for importing!) and thinking about which fields can be nullable.

Once the migration is done, we move on to factories. Here, we define what fake data should look like for our customers—random names, unique emails, sometimes a company, and some sensible defaults for the other fields. This lets us easily generate a batch of test users.

After that, we use php artisan tinker to actually generate (seed) 100 fake customers and push them into the database. Then, we look at how to export all that data as a CSV file—just using our database tool (like Postico), selecting the records, and saving them out. Now we've got a CSV with realistic customer data, ready to test importing (and re-importing) back into our table.

By the end of this episode, we've got sample data and a CSV file prepped and ready to go for the next step: building out the CSV importer itself!

Episode discussion

No comments, yet. Be the first!