Laravel在引号内放入NULL

时间:2018-09-12 08:33:36

标签: php laravel eloquent null quotes

我想与Laravel Eloquent进行联合查询。 当我们进行UNION查询时,两个查询中应有相同数量的选定列。 要跳过该规则,我想选择NULL作为Column_name, 但Laravel API会自动将NULL替换为'Null',这会导致错误“ Null列不存在”。

如何从Null中删除这些自动添加的引号?

这就是我所拥有的: 第一个查询:

...->select("Calendars.*","Services.Id as IdService","Services.Name as ServiceName","NULL as Price")

第二个查询:

...->select("Calendars.*","Services.Id as IdService","Services.Name as ServiceName","PaidService.Price")

结果是:

...union (select `Calendars`.*, `Services`.`Id` as `IdService`, `Services`.`Name` as `ServiceName`, `NULL` as `Price` from `Calendars`

非常感谢!

1 个答案:

答案 0 :(得分:0)

为此考虑使用DB::raw。它将阻止laravel修改语句并按原样对其进行解析。

DB::raw("NULL as Price")

将进行第一个查询

...->select("Calendars.*",
            "Services.Id as IdService",
            "Services.Name as ServiceName",
            DB::raw("NULL as Price"))