你如何为一个雄辩的模型添加别名?例如,如果我有一个SQL查询,如下所示:
SELECT one.name
, one.id
, one.name AS sortkey1
, CAST(NULL AS UNSIGNED) AS sortkey2
, CAST(NULL AS UNSIGNED) AS sortkey3
FROM locations AS one
WHERE one.parent_id = 0
UNION ALL
....
在我的存储库中,我会有以下内容:
$first = $this->model->where('one.parent_id', '=', 0)
->select('one.name'
, 'one.id'
, 'one.name AS sortkey1'
, DB::raw('CAST(NULL AS UNISIGNED) AS sortkey2')
, DB::raw('CAST(NULL AS UNISIGNED) AS sortkey3'));
那么你如何为模型添加别名呢?在上面的示例中,模型映射了位置表,在我的雄辩查询中,我想将其别名为one
,而不是locations
答案 0 :(得分:7)
您可以这样使用from
:
$first = $this->model->from('locations as one')
->where('one.parent_id', '=', 0)
->select('one.name'
, 'one.id'
, 'one.name AS sortkey1'
, DB::raw('CAST(NULL AS UNISIGNED) AS sortkey2')
, DB::raw('CAST(NULL AS UNISIGNED) AS sortkey3'));