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
25 episodes2 hrs 20 mins

Overview

Let's build a powerful CSV importer with Livewire, completely from scratch. This can handle millions of rows, be reused for multiple models, and by using job batches, doesn't require the browser to be open.

This course is for you if:

  • You need a robust importer 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 job batching and queues in Laravel
Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!

Comments

No comments, yet. Be the first to leave a comment.