0

This is the code from my Laravel application:

public function sendNotifications()
    {
        $matchingSubscriptions = DB::table('tournament_match_plan')
            ->join('push_subscriptions', 'push_subscriptions.age_group', '=', 'tournament_match_plan.league')
            ->where('tournament_match_plan.start', '=', '11:20:00')
            ->where('tournament_match_plan.team_1', '=', 'push_subscriptions.club')
            ->orwhere('tournament_match_plan.team_2', '=', 'push_subscriptions.club')
            ->get();

        dd($matchingSubscriptions);
}

Here is the debug message:

Illuminate\Support\Collection {#751 ▼ // app\Http\Controllers\Guests\GuestsPushController.php:97
  #items: []
  #escapeWhenCastingToString: false
}

Why don't I get any result from my Laravel Code?

I've tried the same query in phpMyAdmin:

SELECT *
FROM tournament_match_plan
JOIN push_subscriptions ON push_subscriptions.age_group = tournament_match_plan.league
WHERE tournament_match_plan.start = '11:20:00'
AND (tournament_match_plan.team_1 = push_subscriptions.club OR tournament_match_plan.team_2 = push_subscriptions.club);

With the above code, I get the correct result.

Ro.oT
  • 623
  • 6
  • 15
Stephan
  • 11
  • 3
  • 4
    orWhere needs to have those 2 conditions inside and not on the same level . See https://stackoverflow.com/a/24322732/4964822 – nice_dev Aug 15 '23 at 10:14

1 Answers1

0

Here is the working Code.

        $matchingSubscriptions = DB::table('tournament_match_plan')
        ->join('push_subscriptions', 'push_subscriptions.age_group', '=', 'tournament_match_plan.league')
        ->where('tournament_match_plan.start', '=', '11:20:00')
        ->where(function ($query) {
            $query->where('tournament_match_plan.team_1', '=', DB::raw('push_subscriptions.club'))
                ->orWhere('tournament_match_plan.team_2', '=', DB::raw('push_subscriptions.club'));
        })
        ->get();
Stephan
  • 11
  • 3
  • 5
    Some explanation, so others can learn? What have you changed, what was causing OP's problem? – brombeer Aug 15 '23 at 10:28
  • @brombeer The last `where()->orWhere()` needed parentheses around it. OP should've done `->toSql()` on their query; the difference would have been readily visible. – ceejayoz Aug 18 '23 at 19:34