【问题标题】:Laravel put NULL inside quotesLaravel 将 NULL 放在引号内
【发布时间】:2019-02-16 20:08:17
【问题描述】:

我想用 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`

非常感谢!

【问题讨论】:

    标签: php laravel eloquent null quotes


    【解决方案1】:

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

    DB::raw("NULL as Price")
    

    这将进行第一个查询

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-21
      • 1970-01-01
      • 2022-01-07
      相关资源
      最近更新 更多