【发布时间】:2016-10-30 06:58:54
【问题描述】:
我正在编写查询代码:
$result = \DB::table('order_items as oi')
->selectRaw(
'order_item_type as item_type,
order_item_id as item_id,
if(order_item_type = "App\\\Models\\\Book", oi.price, invoices.price) as price
)
请注意if 语句,我必须使用两个 转义字符或查询与App\Models\Book 不匹配。当我通过 laravel 调试器检查输出查询时:
select order_item_type as item_type,
order_item_id as item_id,
if(order_item_type = "App\\Models\\Book", oi.price, invoices.price) as price,...
这里发生了什么? laravel 查询生成器是否删除一个斜线,然后 mysql 引擎在运行时删除第二个斜线?
编辑:
但是在同一个查询的其他地方我有一个where 子句,我使用了 one 转义字符,它工作正常:
->leftjoin('books', function ($q) {
$q->on('q3.order_item_id', '=', 'books.id')
->where('q3.order_item_type', '=', 'App\\Models\\Book');
})
以及 laravel 调试器的输出查询部分:
left join `books` on `q3`.`order_item_id` = `books`.`id` and
`q3`.`order_item_type` = 'App\Models\Book'
谁能解释为什么在if 中我必须使用两个 转义字符,而在join 中只需要一个 转义字符?
在事实上,如果我什至不在where 查询生成器方法中使用任何转义字符,也没有问题 并将代码编写为:
->leftjoin('books', function ($q) {
$q->on('q3.order_item_id', '=', 'books.id')
->where('q3.order_item_type', '=', 'App\Models\Book');
})
【问题讨论】:
-
您是否尝试过使用 4 个或 2 个反斜杠? 3 个反斜杠对我来说看起来不正确。
-
@IvanYarych 这种行为一直不一样,看看编辑。
标签: mysql laravel query-builder