locnguyen
locnguyen

Reputation: 841

Laravel Compare Multiple Collections?

How can I compare properties of multiple collections. I have the following code.

$games = Schedule::schedules()->where('gameTime', '>', Carbon::now())->get();
// returns a collection with 10+ arrays
$picks = Pick::picks()->where('user_id', 1)->get();
// returns a collection with 0 to 4 arrays

if ($games->first()) {
    foreach ($games as $game) {
        // the following wont work but it's essentially what I want to do
        // compare $game->id to $pick->schedule_id
        if ($game->id == $pick->schedule_id) {
            $pick = Pick::picks()->where('user_id', 1)->where('schedule_id', '=', $game->id)->first();
            $pick->delete();
        }
    }
}
return 'picks deleted';

I want to compare $game->id == $pick->schedule_id inside the foreach loop.

Upvotes: 0

Views: 1467

Answers (1)

Roald D
Roald D

Reputation: 86

It seems that you just want to delete the matching picks and I would suggest a following scenario.

// get the game ids
$games = Schedule::schedules()->where('gameTime', '>', Carbon::now())->lists('id');

// remove the picks
$check = Pick::picks()->where('user_id', 1)->whereIn('schedule_id', $games)->delete();

return 'picks deleted';

Upvotes: 2

Related Questions