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
07. Creating an export record

Transcript

00:00
Before we even start to think about how to export this CSV with all of these records in, or the chosen records, we need to create a permanent record in the database that references this export. The reason we want to do this is because we want to potentially export millions of records and we don't want to wait around for this. So we want to click export,
00:19
go ahead and start the export. We want to be able to exit out of our browser and we want to be able to come back later and click on a menu item up here, see a list of our exports and go ahead and download the file that has eventually been created, because exporting a huge amount of records is going to take some time.
00:37
So with all this in mind, we want to go ahead and first of all, create out a model that represents the export. So let's make out an export model here for the migration and let's go over to the create exports table and let's fill this in. So the first thing and most obvious is that this is going to belong to a specific user.
00:58
So let's add in the user ID here. Next up, of course, we're going to end up with a file. So let's go ahead and add in a string for the file, which could be nullable, because remember when we first generate this, it's going to be null. That will be filled in once the CSV does actually get exported.
01:15
And we also might just want some metadata alongside of this. So we might want an unsigned integer here for the record count. That will just give us an idea of how many records are being exported so we can see these. And then let's also create a timestamp out in here or a date time for the completed act.
01:36
So that's going to be nullable as well, because of course, it's going to be null by default. So that will say when the export was eventually completed. So we'll have a created app when it was started and a completed app when it was finished. With the file and a record count so we can show the user.
01:52
Okay, I think that's just about it. Of course, you can add more to that later if you want to. Let's go ahead and migrate this and let's get this created. And the file name generated as well, which is really important. So step one is going to be actually creating the export app.
02:08
So let's go ahead and reference this as a variable. We're going to say auth user export. So we'll create that relationship in just a second and create. And we want to pass through the record count here,
02:20
which we already know because we created a computed property for that. So we can just reference record count. Okay, so the relationship here for the actual user is pretty straightforward. So let's go ahead and create out an exports relationship here.
02:35
That is just going to be a very simple has many relationship to the export table. Great. Okay, let's just check this out first of all. So let's just make sure when we select 10 transactions and we start that export that actually gets created.
02:53
It doesn't at the moment because we need to go ahead and either unguard our model or go ahead and choose the fillable fields. I'm just going to go ahead and set guarded here to false. Okay, let's try that one more time.
03:06
So let's select 10 of these, export 10, head over to the database, check out our exports. And yeah, there we go. We have a nullable completed out. Of course, it wouldn't have been completed yet. And the file is null because we haven't started the export process.
03:22
Okay, so the record count is looking good. We know that is being filled properly. What else do we need to do here? Well, we need to generate our file name. Now let's think about this because remember a little bit earlier,
03:33
we said that we want to make this export modal work for any model type, regardless of what we're exporting. So at the moment we're working with transactions, but tomorrow we could be working with exporting users.
03:47
So let's go and just pull out here the model that we're working with. So let's say new this model. Now that is different to the builder that we've got in here, up here. We're going to kind of ignore that for now.
04:02
So we've got the model, which is a new instance of the model. What we can do with this is we can grab the table. So we can use that to generate out the file name for the export. Let me show you what I mean.
04:16
So I'm going to say model, get table. And let's just go over. And in fact, I'm going to die dump here before we start to create this record out. So we don't end up with too many records.
04:27
So let's go ahead and say export selected, start export. And there we go, we get transactions. So we can actually use this to prefix the file name that we're about to create with the export.
04:39
So let's say export and update. Now the reason that we're doing this is because what we also want to do is as part of the file name, attach in the created at date of the export. Now we can't really do that unless we have the export prior to this.
04:58
Now what we could do is we could use the current date and time using now. But we could see a differing seconds amount. So I'm just going to go ahead and update this after it's been created. And it would probably be a good idea to move this over a little bit later to an observer
05:16
to hook into the creation process of an export. So this file name always gets generated. So we're basically generating the file name before the file actually exists. So what's the file name going to look like?
05:28
Well, we're going to start it with the table name. So we're going to say get table. Then we're going to go ahead and add on a hyphen. Then we're going to use the string helper to grab the export created at date.
05:43
And we're going to go ahead and just replace out because by default the date looks like this. We get the actual date itself. Then we get the time like this. So what we don't want is colons in the file name.
05:57
We want these to change over to hyphens. So we're just taking the string for the created app. And we're going to go ahead and replace out colons with hyphens. You can do anything you want here.
06:08
This is not the hard and fast rule of how we would generate out this name. And then we're going to end up with the .csv extension. So let's go through the process of exporting again and just make sure that this file name gets generated properly.
06:21
I over to the database and let's check this out. And there we go. So I think that's OK. We could potentially get rid of the space here.
06:30
That kind of makes sense, doesn't it? So let's go and replace out a space as well with a hyphen. And you could use slugify for this. So you could slug the string.
06:44
Yeah, actually let's do that. So let's do slug instead just in case. Kind of makes sense. We can continue chaining on as much as we want.
06:52
OK, let's go ahead and select 10 transactions and start that export. And yeah, there we go. Perfect. So now we have a file name that we know that we want to save the export as on our local file system.
07:06
And we want it downloadable as transactions. So we know where it's come from and the date and time that this was started as well. So you can do anything there. I'm just using that as an example.
07:16
So feel free to change this over as you need. OK, so now that we have created the record, it's time to come down here and export the CSV. Let's go over and look at how we're going to handle that in the next episode.
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!