Using the ToModel, WithValidation while importing excel
when import an excel ,I want to validate each row. How to pass $row to the function rule() ?
I can't find a way to pass param,and I tried to validate data in function model() and throw an exception ,but I can't get the row number I means the row where data didn't pass the validation ,so please tell me a way to get it !
Any additional information, configuration or data that might be necessary to reproduce the issue.
Please see this example in the documentation.
$e->failures() will return an array. Each element of the array will contain the row number and the element which did not pass the validation.
Why was the topic closed?
I think the answer is not correct.
I'm having the same problem, I need to pass a variable to the rules method, but I did not think how to do it.
@rlacerda83 Could you please elaborate on why you think the answer is not sufficient?
Might be best to open a new issue (according to the template) if you have a specific issue you need help with. Would be great if you could add some code too in that case.
Thanks in advance for your reply.
Thanks for the quick response.
My problem is exactly this of the topic, I need to pass a dynamic variable from $row to the rules method.
public function model(array $row)
{
// this should be unique: $row['ccm']
$cliente = Cliente::where('ccm', $row['ccm'])
->where('fk_escritorio', '=', Auth::user()->fk_escritorio)
->first();
$row['usuario_id'] = Auth::user()->isAdmin() ? null : Auth::user()->id;
//create
if (empty($cliente)) {
Log::info('create: ' . $row['razao_social']);
Cliente::create($row);
} else {
//update
Log::info('update: ' . $row['razao_social']);
$cliente->fill($row);
$cliente->save();
}
}
/**
* @return int
*/
public function batchSize(): int
{
return 1500;
}
/**
* @return int
*/
public function chunkSize(): int
{
return 1500;
}
/**
* @return array
*/
public function rules(): array
{
return [
'ccm' => ['required', new UniqueCCM()], // here I need to pass the variable that is in $row['ccm']
'razao_social' => 'required',
];
}
I have the same problem..
Most helpful comment
Thanks for the quick response.
My problem is exactly this of the topic, I need to pass a dynamic variable from $row to the rules method.