0

Laravel Eloquent对于. _ select_ MySQL我的查询是这样的。

SELECT
  *,
  GROUP_CONCAT(p.facility) AS facility
FROM
  (SELECT
    `proposals`.*,
    `proposals`.`desc` AS `pending`,
    `proposals`.`Date` AS `date`,
    `pl`.`type` AS `facility`,
    `ps`.`id` AS `proposalStatusId`,
    `ps`.`status` AS `proposalStatus`
  FROM
    `proposals`
    INNER JOIN
      (SELECT
        *
      FROM
        (SELECT
          *
        FROM
          `proposal_status`
        ORDER BY `created_at` DESC) AS ps
      GROUP BY `ps`.`proposalId`
      HAVING ps.status BETWEEN 4.2
        AND 4.3) AS ps
      ON `proposals`.`id` = `ps`.`proposalId`
    INNER JOIN `proposal_long` AS `pl`
      ON `proposals`.`id` = `pl`.`proposalId`
  WHERE `proposals`.`desc` <> ''
  HAVING `proposals`.`status` = 1) AS p

DB::select()当我使用 in并输出所需的结果时,此查询工作正常。但是当实现如下的雄辩关系时不起作用。

$proposals = Proposals::having('status', DMS::PUBLISH);

$proposalsStatusRaw = proposalStatus::orderBy('created_at', 'desc');

$proposalsStatus = DB::table(DB::raw('(' . $proposalsStatusRaw->toSql() . ') AS ps'))
    ->groupBy('ps.proposalId')
    ->havingRaw('ps.status between ' . DMS::STEP_5 . ' and ' . DMS::STEP_6);

$proposalsRaw = $proposals->select('proposals.*', 'proposals.desc as pending', 'proposals.Date as date', 'pl.type AS facility', 'ps.id AS proposalStatusId', 'ps.status AS proposalStatus')
    ->join(DB::raw('(' . $proposalsStatus->toSql() . ') AS ps'), function ($query) {
        $query->on('proposals.id', '=', 'ps.proposalId');
    })
    ->join('proposal_long AS pl', function ($query) {
        $query->on('proposals.id', '=', 'pl.proposalId');
    })
    ->where('proposals.desc', '<>', '');

$proposals = DB::table(DB::raw('(' . $proposalsRaw->toSql() . ') AS p'))
    ->select('p.*', DB::raw('GROUP_CONCAT(p.facility) AS facility'));

此关系输出与前面提到的相同的查询,但结果为空值。请帮我解决这个错误。

4

1 回答 1

0

替换$proposalsStatusRaw = proposalStatus::orderBy('created_at', 'desc');$proposalsStatusRaw = proposalStatus::all()->orderBy('created_at', 'desc');

尝试$proposalStatusRaw通过添加这个来转储,dd($proposalStausRaw)看看是否有任何结果返回。

于 2015-11-18T09:25:07.530 回答