Laravel eloquent查询为某些列返回null

时间:2017-09-27 21:19:34

标签: php laravel eloquent laravel-5.4 laravel-eloquent

我有以下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生成的RAW 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

查询运行正常,但对于结果的每一行,为home,draw,away,over和under列返回的值为null。然而,如果我得到Eloquent刚刚运行的原始SQL并在PhpMyAdmin中执行它,则所有这些字段都具有值。为什么Laravel在有值时会为它们返回null?

更新

通过在查询之前放置\ DB :: enableQueryLog然后在dd(\ DB :: getQueryLog()中获取以下原始SQL,当在PhpMyAdmin中运行时,执行与Laravel相同的操作并为这些列返回null,以便这个查询必须有所不同。

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

1 个答案:

答案 0 :(得分:0)

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

    Cell[][] sea = blockadeMap.getSea();
    Wind[][] winds = blockadeMap.getWinds();
    Whirlpool[][] whirls = blockadeMap.getWhirls();

    for (int i = 0; i < sea.length; i++) {
        for(int j = 0; j < sea[i].length; j++) {
            TextureRegion region = sea[i][j].getRegion();

            int x = (i * GameTile.TILE_WIDTH / 2) - (j * GameTile.TILE_WIDTH / 2) - region.getRegionWidth() / 2;
            int y = (i * GameTile.TILE_HEIGHT / 2) + (j * GameTile.TILE_HEIGHT / 2) - region.getRegionHeight() / 2;

            batch.draw(region, x, y);

            if (winds[i][j] != null) {
                region = winds[i][j].getRegion();

                x = (i * GameTile.TILE_WIDTH / 2) - (j * GameTile.TILE_WIDTH / 2) - region.getRegionWidth() / 2;
                y = (i * GameTile.TILE_HEIGHT / 2) + (j * GameTile.TILE_HEIGHT / 2) - region.getRegionHeight() / 2;

                batch.draw(region, x, y);
            }
            else if (whirls[i][j] != null) {
                region = whirls[i][j].getRegion();

                x = (i * GameTile.TILE_WIDTH / 2) - (j * GameTile.TILE_WIDTH / 2) - region.getRegionWidth() / 2;
                y = (i * GameTile.TILE_HEIGHT / 2) + (j * GameTile.TILE_HEIGHT / 2) - region.getRegionHeight() / 2;

                batch.draw(region, x, y);
            }
        }
    }

应该是:

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