Laravel查询不工作,但与phpMyadmin中的查询相同的代码可以工作

8wigbo56  于 2023-10-20  发布在  PHP
关注(0)|答案(1)|浏览(151)

下面是我的Laravel应用程序的代码:

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);
}

下面是调试消息:

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

为什么我的Laravel代码没有任何结果?
我在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);

通过上面的代码,我得到了正确的结果。

clj7thdc

clj7thdc1#

下面是工作代码。

$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();

相关问题