Validation error message do not match log error message in Laravel excel validation

How can I resolve an issue where my log error messages are correct, but the validation error messages displayed to the user show a different dose number than expected? Here’s my code for logging and validating doses for vaccines, but the row number in the validation errors doesn’t match the log output.


public function withValidator($validator)
    {
        $validator->after(function ($validator) {
            $data = $validator->getData();

            foreach ($data as $key => $row) {
                $vaccine = $row['vaccine'];
                Log::info('$vaccine'.$vaccine);
                $inventory = Inventory::where('name', $vaccine)->where('inventory_type_id', 15)->first();

                if (!$inventory) {
                    Log::error("The vaccine '{$vaccine}' does not exist.");
                    $validator->errors()->add("vaccine", "The vaccine '{$vaccine}' does not exist.");
                } else if (isset($row['given_doses']) && (int)$row['given_doses'] > (int)$inventory->dose_numbers) {
                    $totalDoses=$inventory->dose_numbers;
                    $message="The DOSE set for '$inventory->name.' is  '$totalDoses' any dose given above '{$totalDoses}' should not be added.";
                    Log::info("Dose numbers for '{$inventory->name}': {$totalDoses}");
                    Log::info("Processing row {$key}: Vaccine ' {$vaccine}', Given Doses: '{$row['given_doses']}', Inventory Dose Numbers: '{$inventory->dose_numbers}'");

                    Log::error($message);
                    $validator->errors()->add($key, $message);
                }
            }
        });
    }

Here is the output of my log

[2024-09-05 13:59:08] local.INFO: Dose numbers for ‘FMD’: 25
[2024-09-05 13:59:08] local.INFO: Processing row 2: Vaccine ‘ FMD’, Given Doses: ’30’, Inventory Dose Numbers: ’25’
[2024-09-05 13:59:08] local.ERROR: The DOSE set for ‘.FMD.’ is ‘.25.’ any dose given above ’25’ should not be added.

and here is the message displayed in the view
There was an error on row 2. The DOSE set for ‘FMD.’ is ‘5’ any dose given above ‘5’ should not be added.

It should print the exact numbers of doses in both messages. Log is displaying corrext values and view is not.