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
13. Fetching the CSV records

Transcript

00:00
in this episode we're going to tweak around how we read our csv and we're going to make use of computed properties within livewire firstly for a caching benefit and second of all it's just going to make it a little bit easier to read these again and again so we might need to read them
00:16
again later when we start the queue process so we're just going to tweak this around what this will also mean is once we've got to the point where we can actually read records we can grab the total amount of rows so at the moment what we're doing is directly accessing this read csv method what i'm actually going to do and we can always change this later if it doesn't work out
00:35
is get or create a get read csv property or a get read csv property now what this is going to do is exactly the same thing it's going to return to us a reader that we've already seen but it's basically just going to do what we've done here so i'm going to go ahead and grab this replace this with this red csv as a property and we're just going to return exactly what we've done
01:00
from here so that's going to work in exactly the same way we can test this out by just importing another file and we should just see everything that we already see with the columns in the drop down great so that's working nicely now what we want to do though is create another computed property to access the records from this csv so we're going to go ahead and say get csv
01:20
records property and we're going to go ahead and return this is slightly different so again using the library that we've already pulled in from league we're going to say statement so we're going to pull in the statement object from here we're going to say create and we're going to process the red csv so that's just this red csv so what this is going to do is the property is
01:43
going to read the records for us and we'll be able to iterate over the records it's not quite as simple as that when we get to it because we're going to need to batch them and what this actually returns to us is a php generator so we'll speak about that in a little bit more detail later but at least now that we've got the csv records property now we can come down to our total rows
02:05
and we can count on this csv records because we have the records that we can iterate over and we can use laravel's native count to grab the count of them so what we can now do is create another import and see if this gives us the records that we have now we know we've got 100 in the one that we exported earlier so let's go over import this and see what we get so i'm just
02:29
going to map up each of these really quickly and hit import come over and there we go we've got a total rows of 100 so we're already pretty much there and now reading the records of course what we need to do now though is access these records put them into a queue in batches or in chunks and process them

Episode summary

In this episode, we focus on improving the way we read CSV files in our Livewire component. Previously, we were directly calling a method to read the CSV, but now we're refactoring this to use a computed property. This not only gives us some free caching benefits, but also makes it clearer and easier to reuse reading logic later—helpful since we'll need to access the CSV data multiple times (for example, when we kick off batching and queue processing).

First, we set up a getReadCsvProperty so whenever we need the CSV reader, we reference this property instead of the direct method. This keeps our code a bit cleaner and lays the groundwork for further improvements.

Next, we add another computed property for getting the actual CSV records. Using the League CSV library we're already using, we pipe our reader through a Statement to get an iterable set of records. It’s worth noting that this gives us a PHP generator, so there are some nuances here we'll cover in more detail later—especially when it comes to batching the records for processing in chunks.

Finally, we connect everything up so that we can quickly count the total rows in the imported CSV. We test it out by importing a file and confirming the row count shows as expected. With this foundation set, we're now ready to move onto chunking these records and putting them into a queue for background processing in the next steps!

Episode discussion

No comments, yet. Be the first!