Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 2 years ago.
0

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) {

        }

    }
}
0

@thomastkim

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?

0

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
        }

    }
}
Last updated 9 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

eldian eldian Joined 10 Oct 2015

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.