Laravel eloquent 查询为某些列返回 null

Laravel eloquent query returning null for some columns

我有以下 Eloquent 查询:

$query = Event_model::join('countries', function($q) use ($enabled_country_ids) {
        $q->on('events.country_id', '=', 'countries.id')
          ->whereIn('countries.id', $enabled_country_ids);
    })
    ->join('leagues', function($q) use ($enabled_league_ids) {
        $q->on('events.league_id', '=', 'leagues.id')
          ->whereIn('leagues.id', $enabled_league_ids); 
    })
    ->leftJoin('event_odds AS eo1', function($q) use($win_market, $home_market_value) {
        $q->on('events.id', '=', 'eo1.event_id')
          ->where('eo1.market_id', '=', $win_market->id)
          ->where('eo1.market_value_id', '=', $home_market_value->id)
          ->where('eo1.value', '=', 'eo1.best_odd');
    })
    ->leftJoin('event_odds AS eo2', function($q) use($win_market, $draw_market_value) {
        $q->on('events.id', '=', 'eo2.event_id')
          ->where('eo2.market_id', '=', $win_market->id)
          ->where('eo2.market_value_id', '=', $draw_market_value->id)
          ->where('eo2.value', '=', 'eo2.best_odd');
    })
    ->leftJoin('event_odds AS eo3', function($q) use($win_market, $away_market_value) {
        $q->on('events.id', '=', 'eo3.event_id')
          ->where('eo3.market_id', '=', $win_market->id)
          ->where('eo3.market_value_id', '=', $away_market_value->id)
          ->where('eo3.value', '=', 'eo3.best_odd');
    })
    ->leftJoin('event_odds AS eo4', function($q) use($ou_market, $over_market_value) {
        $q->on('events.id', '=', 'eo4.event_id')
          ->where('eo4.market_id', '=', $ou_market->id)
          ->where('eo4.market_value_id', '=', $over_market_value->id)
          ->where('eo4.value', '=', 'eo4.best_odd');
    })
    ->leftJoin('event_odds AS eo5', function($q) use($ou_market, $under_market_value) {
        $q->on('events.id', '=', 'eo5.event_id')
          ->where('eo5.market_id', '=', $ou_market->id)
          ->where('eo5.market_value_id', '=', $under_market_value->id)
          ->where('eo5.value', '=', 'eo5.best_odd');
    })
    ->whereIn('events.sport_id', $enabled_sports_ids)
    ->where('events.event_datetime', '>', time())
    ->select([
        'events.id',
        DB::raw('FROM_UNIXTIME(events.event_datetime, "%a %D %b %H:%i") AS datetime'),
        DB::raw('CONCAT(countries.name, " ", leagues.name, " | ", FROM_UNIXTIME(events.event_datetime, "%Y-%m-%d")) AS countryleague'),
        DB::raw('CONCAT(events.team1_name, " vs ", events.team2_name) AS game'),
        'eo1.value AS home',           
        'eo2.value AS draw',
        'eo3.value AS away',
        'eo4.value AS over',
        'eo5.value AS under',
        DB::raw('CONCAT("<a href=\"#\" class=\"expand-all btn\" data-id=\"", events.id, "\"><i class=\"fa fa-plus\"></i> Expand all odds</a>") AS expand'),
        'leagues.major'
    ]);

这是 Eloquent 生成的原始 SQL 查询:

SELECT
  `events`.`id`,
  FROM_UNIXTIME(
    EVENTS.event_datetime,
    "%a %D %b %H:%i"
  ) AS DATETIME,
  CONCAT(
    countries.name,
    " ",
    leagues.name,
    " | ",
    FROM_UNIXTIME(
      EVENTS.event_datetime,
      "%Y-%m-%d"
    )
  ) AS countryleague,
  CONCAT(
    EVENTS.team1_name,
    " vs ",
    EVENTS.team2_name
  ) AS game,
  `eo1`.`value` AS `home`,
  `eo2`.`value` AS `draw`,
  `eo3`.`value` AS `away`,
  `eo4`.`value` AS `over`,
  `eo5`.`value` AS `under`,
  CONCAT(
    "<a href=\"#\" class=\"expand-all btn\" data-id=\"",
    EVENTS.id,
    "\"><i class=\"fa fa-plus\"></i> Expand all odds</a>"
  ) AS expand,
  `leagues`.`major`
FROM
  `events`
INNER JOIN
  `countries` ON `events`.`country_id` = `countries`.`id` AND `countries`.`id` IN(1,2,3,4)
INNER JOIN
  `leagues` ON `events`.`league_id` = `leagues`.`id` AND `leagues`.`id` IN(1,2,3,4)
LEFT JOIN
  `event_odds` AS `eo1` ON `events`.`id` = `eo1`.`event_id` AND `eo1`.`market_id` = 3 AND `eo1`.`market_value_id` = 51 AND `eo1`.`value` = eo1.best_odd
LEFT JOIN
  `event_odds` AS `eo2` ON `events`.`id` = `eo2`.`event_id` AND `eo2`.`market_id` = 3 AND `eo2`.`market_value_id` = 52 AND `eo2`.`value` = eo2.best_odd
LEFT JOIN
  `event_odds` AS `eo3` ON `events`.`id` = `eo3`.`event_id` AND `eo3`.`market_id` = 3 AND `eo3`.`market_value_id` = 53 AND `eo3`.`value` = eo3.best_odd
LEFT JOIN
  `event_odds` AS `eo4` ON `events`.`id` = `eo4`.`event_id` AND `eo4`.`market_id` = 4 AND `eo4`.`market_value_id` = 54 AND `eo4`.`value` = eo4.best_odd
LEFT JOIN
  `event_odds` AS `eo5` ON `events`.`id` = `eo5`.`event_id` AND `eo5`.`market_id` = 4 AND `eo5`.`market_value_id` = 55 AND `eo5`.`value` = eo5.best_odd
WHERE
  `events`.`sport_id` IN(1) AND `events`.`event_datetime` > 1506546556
ORDER BY
  `leagues`.`major` DESC,
  `events`.`event_datetime` ASC,
  `leagues`.`name` ASC
LIMIT 60 OFFSET 0

查询 运行 没问题,但对于结果的每一行,主场、平局、客场、上方和下方列的返回值为空。然而,如果我得到 Eloquent 的原始 SQL 只是 运行 并在 PhpMyAdmin 中执行它,所有这些字段都有值。为什么 Laravel 在它们确实有值时为它们返回 null?

更新

通过在查询之前放置 \DB::enableQueryLog 然后 dd(\DB::getQueryLog() 我得到以下原始 SQL 当 运行 在 PhpMyAdmin 中做同样的事情Laravel 对这些列做了 returns 为空,因此此查询一定有不同之处。

SELECT
  `events`.`id`,
  FROM_UNIXTIME(
    EVENTS.event_datetime,
    "%a %D %b %H:%i"
  ) AS DATETIME,
  CONCAT(
    countries.name,
    " ",
    leagues.name,
    " | ",
    FROM_UNIXTIME(
      EVENTS.event_datetime,
      "%Y-%m-%d"
    )
  ) AS countryleague,
  CONCAT(
    EVENTS.team1_name,
    " vs ",
    EVENTS.team2_name
  ) AS game,
  CONCAT(eo1.value) AS home,
  `eo2`.`value` AS `draw`,
  `eo3`.`value` AS `away`,
  `eo4`.`value` AS `over`,
  `eo5`.`value` AS `under`,
  CONCAT(
    "<a href=\"#\" class=\"expand-all btn\" data-id=\"",
    EVENTS.id,
    "\"><i class=\"fa fa-plus\"></i> Expand all odds</a>"
  ) AS expand,
  `leagues`.`major`
FROM
  `events`
INNER JOIN
  `countries` ON `events`.`country_id` = `countries`.`id`
INNER JOIN
  `leagues` ON `events`.`league_id` = `leagues`.`id`
LEFT JOIN
  `event_odds` AS `eo1` ON `events`.`id` = `eo1`.`event_id` AND `eo1`.`market_id` = 3 AND `eo1`.`market_value_id` = 51 AND `eo1`.`value` = eo1.best_odd
LEFT JOIN
  `event_odds` AS `eo2` ON `events`.`id` = `eo2`.`event_id` AND `eo2`.`market_id` = 3 AND `eo2`.`market_value_id` = 52 AND `eo2`.`value` = eo2.best_odd
LEFT JOIN
  `event_odds` AS `eo3` ON `events`.`id` = `eo3`.`event_id` AND `eo3`.`market_id` = 3 AND `eo3`.`market_value_id` = 53 AND `eo3`.`value` = eo3.best_odd
LEFT JOIN
  `event_odds` AS `eo4` ON `events`.`id` = `eo4`.`event_id` AND `eo4`.`market_id` = 4 AND `eo4`.`market_value_id` = 54 AND `eo4`.`value` = eo4.best_odd
LEFT JOIN
  `event_odds` AS `eo5` ON `events`.`id` = `eo5`.`event_id` AND `eo5`.`market_id` = 4 AND `eo5`.`market_value_id` = 55 AND `eo5`.`value` = eo5.best_odd
WHERE
  `events`.`sport_id` IN(1) AND `events`.`event_datetime` > 0
ORDER BY
  `leagues`.`major` DESC,
  `events`.`event_datetime` ASC,
  `leagues`.`name` ASC
LIMIT 60 OFFSET 0

啊啊啊我破解了!这是因为我在连接中使用的是 WHERE 而不是 ON 例如

->leftJoin('event_odds AS eo1', function($q) use($win_market, $home_market_value) {
    $q->on('events.id', '=', 'eo1.event_id')
      ->where('eo1.market_id', '=', $win_market->id)
      ->where('eo1.market_value_id', '=', $home_market_value->id)
      ->where('eo1.value', '=', 'eo1.best_odd');
})

应该是:

->leftJoin('event_odds AS eo1', function($q) use($win_market, $home_market_value) {
    $q->on('events.id', '=', 'eo1.event_id')
      ->where('eo1.market_id', '=', $win_market->id)
      ->where('eo1.market_value_id', '=', $home_market_value->id)
      ->on('eo1.value', '=', 'eo1.best_odd');
})