I'm using the following package https://laravel-excel.com/ to import excel files but have run into a problem while validating the rows.
The header row names might be different from the column names in the database. So my question is how can I validate the header rows with a different name? For example the database column name is first_name
but the header row name can be either first_name
or voornaam
.
I was also wondering why the :message
isn't using the localization :attribute
from the language files. For example the attribute first_name
is translated in lang/nl/validation.php as voornaam
the error message still returns first_name
.
The import function:
public function model( array $row )
{
return new Participant([
'salutation' => $row['salutation'] ?? $row['aanhef'] ?? null,
'first_name' => $row['first_name'] ?? $row['voornaam'] ?? $row['voorletters'] ?? $row['voorletter'],
'last_name' => $row['last_name'] ?? $row['achternaam']
]);
}
The validation rules:
public function rules(): array
{
return [
'salutation' => 'nullable|string',
'first_name' => 'required|string',
'last_name' => 'required|string',
]
}
I have tried to use the customValidationAttributes
but that doesn't work. Also made a issue about it but so far no response.
public function customValidationAttributes()
{
return [
'salutation' => 'salutation',
'aanhef' => 'salutation',
'first_name' => 'first_name',
'voornaam' => 'first_name',
'last_name' => 'last_name',
'achternaam' => 'last_name'
]
}