Laravel 5选择并列出关系

时间:2015-04-28 18:17:40

标签: list select laravel-5

我在Laravel 5上遇到了一些问题,我需要将此查询返回到列表:

SELECT CONCAT(pmt_location.localName,
    IF (LENGTH(pmt_location.dialCode) > 0,
        CONCAT( ' (+', pmt_location.dialCode,')'
        ),
      ''
    )
    ) AS localName,
    pmt_location.id
FROM
    pmt_location
WHERE
    pmt_location.type = 'CO'
ORDER BY
    pmt_location.sOrder ASC,
    pmt_location.localName ASC

我是这样尝试但是它仍然不对:

$query = DB::select("SELECT CONCAT(pmt_location.localName, IF ( LENGTH(pmt_location.dialCode) > 0, CONCAT(' (+', pmt_location.dialCode, ')'), '' )) AS localName, pmt_location.id FROM pmt_location WHERE pmt_location.type = 'CO' ORDER BY pmt_location.sOrder ASC, pmt_location.localName ASC");

return $query->lists('localName', 'id');

1 个答案:

答案 0 :(得分:0)

我自己找到了解决方案,也许会帮助其他新手:P。

return DB::table('pmt_location')
    ->select(array("pmt_location.id", DB::raw("CONCAT( pmt_location.localName, IF ( LENGTH(pmt_location.dialCode) > 0, CONCAT(' (+', pmt_location.dialCode, ')'), '' )) AS localName")))
    ->whereRaw("pmt_location.type = 'CO'")
    ->orderBy('sOrder')
    ->orderBy('localName')
    ->lists('localName', 'id');