I using laravel excel Maatwebsite.
public function collection(Collection $rows)
{
Validator::make($rows->toArray(), [
'*.price' => 'numeric',
])->validate();
}
I need output
Excel Row No not array number
Row No 1.price must be a number.
2
Answers
as I understand you need to validate uploaded excel rows.
the package
excel Maatwebsite
provided validation rules feature too.in your import class you need to add
WithValidation
,WithHeadingRow
interface andrules
mothod. in this way uploaded excel will be validated before insert into database:and to gather errors:
now
$failures
contains all validation error in all rowsThis example use interface Validator that take different arguments to initialize;
(array $data, array $rules, array $messages, array $customAttributes)
to validate rows and referenced from web sources, do not implementToModel
concern and is similar to the example showed in the question.Another scenario like defined a custom data or Excel file like the following, suppose you want to access specific cells, you can implement the
WithMappedCells
concern.