12345678910111213141516171819202122232425262728293031 |
- <?php
- namespace App\Imports;
- use App\Http\Controllers\JsonResponseHelper;
- use Illuminate\Support\Collection;
- use Illuminate\Support\Facades\Validator;
- trait ImportValidatorHelper
- {
- use JsonResponseHelper;
- protected function validatorByCollection(Collection $collection, array $rules, array $messages = [])
- {
- $errors = [];
- foreach ($collection as $index => $item) {
- $validator = Validator::make($item->toArray(), $rules, $messages);
- if ($validator->fails()) {
- $errors[$index + 1] = [
- 'index' => $index + 1,
- 'errors' => $validator->errors()->all()
- ];
- }
- }
- if ($errors) {
- throw new \App\Exceptions\ValidationException(errors: array_values($errors));
- }
- }
- }
|