You can loop through the input array and compare each value with its corresponding max value in the database collection. Here's an example:
$input = [
22 => ["value" => 100.5],
103 => ["value" => 85.3],
221 => ["value" => 20.22]
];
$dataFromDB = MyModel::whereIn('id', array_keys($input))->get();
foreach ($input as $id => $value) {
$maxValue = $dataFromDB->where('id', $id)->first()->max_value;
if ($value['value'] > $maxValue) {
// handle validation error
}
}
In this example, we loop through each key-value pair in the input array and use the where method to filter the database collection by the corresponding ID. We then use the first method to get the first matching record and access its max_value property. Finally, we compare the input value with the max value and handle any validation errors as needed.