You need to loop through the collection of picks as well.
foreach ($games as $game) {
foreach ($picks as $pick) {
// Do your comparison here
if ($game->id == $pick->schedule_id) {
}
}
}
Your solution works but now it will loop through many more times. Is there another solution where it doesn't loop through so many times unnecessarily?
If you are deleting the pick, I guess you can use the collection's forget
method to remove it from the collection so that next time, it no longer loops through that.
foreach ($games as $game) {
foreach ($picks as $key => $pick) { // Note: $key represents the current element's key so the first one will be 0, second one will be 1, etc.
// Do your comparison here
if ($game->id == $pick->schedule_id) {
$picks->forget($key); // Removing the pick from the collection using that $key
$pick->delete(); // Deleting it from the database as well
}
}
}
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community