Laravel在我的orderBy周围添加单引号,导致查询无法按预期执行
我尝试从我的select语句中删除单引号时尝试了多种使用DB :: raw的组合,并且无处可去。
<?php
$sql = "SELECT cust_name, ad_text, total_sms FROM customers WHERE created_at > :startDate AND created_at < :endDate ORDER BY :orderBy DESC;";
return DB::Select($sql, ['startDate'=>$startDate,'endDate'=>$endDate, 'orderBy' => $orderBy]);?>
来到
ORDER BY 'total_sms' DESC;
如何转义此绑定参数,以便删除其单引号?
答案 0 :(得分:1)
在“符号”之间添加列名,而不是“'”。
str_replace("'", "\\'", $str)
答案 1 :(得分:0)
另一个好方法是
DB::table("customers as c")
->whereBetween("created_at",[$start_date, $end_date])
->select("cust_name", "ad_text", "total_sms")
->orderBy($orderBy,"Desc")
->get();