-
ProblemValidating a collection of items - CSV imports, bulk API payloads, queue-based processors - has no first-class solution in Laravel. Today every developer writes the same loop: $errors = [];
foreach ($rows as $i => $row) {
$validator = Validator::make($row, $rules);
if ($validator->fails()) {
$errors[$i] = $validator->errors();
}
}This pattern is repeated in every import job, every bulk endpoint, every test fixture loader. There's no standard structure for the error collection, no lazy support, and no unified exception. The existing Proposed API$result = Validator::collection($rows, [
'email' => ['required', 'email'],
'age' => ['required', 'integer', 'min:0'],
]);
if ($result->hasErrors()) {
// Collection<int, MessageBag> keyed by row index
$result->errors();
// Only the rows that failed
$result->failed();
// Throw a single exception carrying all per-row errors
$result->throwIfFailed();
}
// Only the rows that passed - safe to persist
$result->passed()->each(fn ($row) => User::create($row));With lazy support: // Validates lazily - cursor is never fully materialized
$result = Validator::collection(User::cursor(), $rules);Implementation Sketch
Estimated diff: ~160 lines across 3–4 files. I'm happy to implement if the API direction makes sense. Open Design Questions
Happy to hear if there's a simpler shape you'd prefer before I start writing code. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
|
You can do this today already without $validator = Validator::make(['rows' => $rows], [
'rows.*.email' => ['required', 'email'],
'rows.*.age' => ['required', 'integer', 'min:0'],
]);
if ($validator->fails()) {
$errors = $validator->errors();
} |
Beta Was this translation helpful? Give feedback.
Taylor never replies to discussions