Can it be first letter case issue? Your column is Batch while rule refers to batch. Try to change the rule:
'beans' => ['exclude_if:Batch,SUM', ...
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hello, Using laravel 12 and the package: Laravel Excel.
https://docs.laravel-excel.com/3.1/imports/validation.html
I have an excel file with these column names:
Batch, Time, Profile, Beans, In (g), Out (g)
I need to validate rows only if the value in the Batch column is not: SUM
The problem I have is that the validations happen even if the value in the column is SUM. I get the errors on the row where the value in the column Batch is SUM.
I tried 3 different ways:
My import class:
<?php
namespace App\Imports;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
use Maatwebsite\Excel\Concerns\WithValidation;
use App\Models\Coffee;
use App\Models\GreenBean;
use Illuminate\Support\Facades\Log;
class RoastImport implements ToCollection, WithHeadingRow, WithValidation
{
private $bean;
/**
* @param Collection $rows
*/
public function collection(Collection $rows)
{
foreach ($rows as $row) {
$name = $row['profile'];
$weight = $row['out_g'];
}
}
public function rules(): array
{
return [
'beans' => ['exclude_if:batch,SUM', 'required', 'exists:green_beans,name'],
'out_g' => ['exclude_if:batch,SUM', 'required', 'numeric'],
];
//I also tried:
return [
'out_g' => Rule::when([fn($input) => $input->batch != 'SUM', ['required', 'numeric']),
'beans' => Rule::when(fn($input) => $input->batch != 'SUM', ['required', 'exists:green_beans,name']),
]
//and
return [
'out_g' => Rule::when(fn($input) => $input['batch'] != 'SUM', ['required', 'numeric']),
'beans' => Rule::when(fn($input) => $input['batch'] != 'SUM', ['required', 'exists:green_beans,name']),
];
}
}
Please help.
Look here: https://docs.laravel-excel.com/3.1/imports/validation.html#validating-with-a-heading-row
If your validation rules reference other field names, ... the field name must be prefixed with *.
So try this:
'beans' => ['exclude_if:*.batch,SUM', ...
Please or to participate in this conversation.