search

Home  >  Q&A  >  body text

Laravel query fails but same code works in PhpMyadmin

<p>This is the code for my Laravel application: </p> <pre class="brush:php;toolbar:false;">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); }</pre> <p>This is the debugging result:</p> <pre class="brush:php;toolbar:false;">IlluminateSupportCollection {#751 ▼ // appHttpControllersGuestsGuestsPushController.php:97 #items: [] #escapeWhenCastingToString: false }</pre> <p>Why is my Laravel code getting no results? </p> <p>I tried the same query in PhpMyAdmin: </p> <pre class="brush:php;toolbar:false;">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);</pre> <p>Using this query I got a result, and it was correct. </p>
P粉111627787P粉111627787477 days ago459

reply all(1)I'll reply

  • P粉198749929

    P粉1987499292023-08-16 09:07:16

    The following 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();

    reply
    0
  • Cancelreply