let's go ahead and improve on not only the columns that we have here but also the validation rule columns because at the moment when we say the columns to map dot first name field is required it doesn't really make too much sense so the first thing that we're going to do is head over to the index here where we've added our required columns and our columns to map and we're going to
00:21
add in our column labels and again you can call this whatever you want but let's go ahead and map these in so for example let's say id should be displayed as id first underscore name should be displayed as first name and exactly the same for any of the others that you want to include so i'll include the last name here and then finally we'll go ahead and add in the email so
00:45
we've now got our column labels and let's just put that to singular so if we head over to our csv importer again go all the way up to the top here we'll just add these in like we've done with the others and add in their column labels and that's pretty much them added into our component so now what we can do is over in our csv importer rather than when we see our column just here just access
01:10
the actual column itself we can go ahead and supply the data that we have from here so we can change this to column labels and then inside of this array access the column as the key and that should give us the value out now there's a chance that the column label might not exist based on the columns that you've got so a good idea would be to fall back just to the default column name let's
01:34
take a look and see what this looks like we'll go ahead and hit import here upload this file and choose our customers and there we go these are all nicely mapped up the next thing we want to focus on are the validation messages these are a little bit more tricky but it's nothing we haven't already done now within livewire as well as providing the rules method we can also provide
01:54
out a method that gives us the validation attributes that we want as well as any custom messages so i'm going to go ahead and create out the validation attributes method in here and let's go ahead and just return an array just to test this for now so this will for example say columns to map dot email that should be displayed as email rather than columns to map dot email
02:19
so let's just try this out again go ahead and import a file here and we'll go ahead and submit this and there we go you can see the email field is required so that's a massive improvement but we need to dynamically build this up because we don't know what's being passed into this component it can be different uh regardless of the depending on the data that we've already got so what we're
02:40
going to do is again collect up the data that we've passed in so this required columns because we only want to generate it for our required columns and then we're again going to map through this with keys and just build up the kind of array that we want to see so in here let's go ahead and add this closure we'll get a column for each of their mappings and we're going to return an array
03:01
that looked pretty much like we just did but of course it's going to be dynamic so columns to map dot and then we want to append the column on and now we want to go ahead and access out if we just do that inside of here the column label so that is just going to be this column labels and then the column and then of course what we can do is fall back again to the column that we're mapping
03:24
through just in case that doesn't exist so that's pretty much just going to go through and build up an array of what we manually did here but with all the data we've seen and this is very similar to what we did here okay let's try this out so i'm going to go ahead and give this a refresh hit import upload a file go over to customers and let's hit import and there we go the id field is
03:46
required the first name field is required and so on now of course what you could do is you could lowercase this if you wanted to so you could use string to lower that's what you wanted to do in there that might make a little bit more sense for the validation message so let's just check that out it's entirely up to you how you format this but that will probably look a little bit better
25 episodes•2 hrs 20 mins•2 years ago
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