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


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


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!


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