Good day! I would like to know if this is possible to be refactored into a cleaner code. The reason I'm doing it like this is to catch the column that the error appeared on and then potentially output the column name and value for the client to view later on.
// Make the name act as the key and the value equates to ID
// $towers['tower1'] = 1;
$towers = Tower::pluck('id', 'name')->toArray();
$departments = Department::pluck('id', 'name')->toArray();
foreach($data as $key => $row) {
try {
// $row['tower'] = 'Hello World Tower'
// Try to see if $row['tower'] is present in the $towers data source
$data[$key]['tower'] = $towers[$row['tower']];
} catch (\Exception $e) {
// Exception occurs because index is not available
$error_holder[$key]['tower_error'] = $e->getMessage();
}
// Same logic as above
try {
$data[$key]['department'] = $departments[$row['department']];
} catch (\Exception $e) {
$error_holder[$key]['department_error'] = $e->getMessage();
}
}