Laravel 将 NULL 放在引号内
Laravel put NULL inside quotes
我想与 Laravel Eloquent 进行联合查询。
当我们进行 UNION 查询时,两个查询中应该有相同数量的 selected 列。
要跳过该规则,我想将 select 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`
非常感谢!
考虑为此使用 DB::raw。它将阻止 laravel 修改语句并按原样解析它。
DB::raw("NULL as Price")
这将进行第一个查询
...->select("Calendars.*",
"Services.Id as IdService",
"Services.Name as ServiceName",
DB::raw("NULL as Price"))
我想与 Laravel Eloquent 进行联合查询。
当我们进行 UNION 查询时,两个查询中应该有相同数量的 selected 列。
要跳过该规则,我想将 select 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`
非常感谢!
考虑为此使用 DB::raw。它将阻止 laravel 修改语句并按原样解析它。
DB::raw("NULL as Price")
这将进行第一个查询
...->select("Calendars.*",
"Services.Id as IdService",
"Services.Name as ServiceName",
DB::raw("NULL as Price"))