Hi @neeraj1005
I've made a helper function for those cases as I had several imports for one client:
public function validateHeaders($valid, $imported)
{
if($imported->diffAssoc($valid)->count() > 0)
{
foreach($imported->diffAssoc($valid) as $field) {
$errors[$field] = "Wrong header: ".$field." (valid headers: ".implode(", ", $valid).")";
}
throw ValidationException::withMessages($errors);
}
return true;
}
Then, at each Import Class I define an array with valid headers and pass it as well as the heading row to my helper function:
private $headers = ['product_name', 'description', 'notes', 'team'];
public function collection(Collection $rows)
{
validateHeaders($this->headers, $rows[0]->keys());
// rest of your code
}
The helper function will throw a custom ValidationException so it will behave the same as the rest of the validations.
As you can see, in my case I use it with ToCollection importers instead of ToModel, but in case you don't want to change to ToCollection, I guess you can import the headings row as suggested here to pass it to check against your valid ones: https://docs.laravel-excel.com/3.1/imports/heading-row.html#importing-only-the-heading-row
Hope this helps!