Is there a more declarative / less horrible way of writing this (deliberately simplified) code?
I realise I can use ->each()
, but that still doesn't get rid of the nesting in this case?
Note I do need to generate all the combinations, and I have a mixture of things to loop through - configuration data, ordinary arrays, Eloquent, though obviously I could convert them first...
foreach(config('app.goals') as $goal) {
foreach(config('app.age_groups') as $ages) {
foreach($regions as $region_name => $region_id) {
foreach($interests->pluck('name')->prepend('') as $interest) {
foreach(config('app.devices') as $device) {
$record = new Foo();
$record->goal = $goal;
$record->age = $age;
$record->region = $region_id;
$record->interest = $interest;
$record->device = $device;
$record->save();
}
}
}
}
}
Unclear if there's a Collection method that can help? e.g.
holygrail(config('app.goals'),config('app.age_groups'),$regions...)->each(function($combination) {
// logic for every combination
});