Laravel中的查询失败但在MySQL中有效

时间:2014-05-15 16:00:41

标签: php mysql laravel laravel-4 eloquent

我在Eloquent模型中定义了一些查询范围方法,并在控制器中调用它们以允许Laravel使用

构建查询
return View::make('credit_apps/view')->with('creditApps', CreditApp::ApprovalStatus()
                                     ->SalesRep()
                                     ->Submitted()
                                     ->RefReceived()
                                     ->get()
                                     );

Illuminate返回的错误是MySQL语法错误:

Illuminate \ Database \ QueryException SQLSTATE[42000]: Syntax error
or access violation: 1064 You have an error in your SQL syntax; check
the manual that corresponds to your MySQL server version for the right
syntax to use near '' at line 1 (SQL: select * from `credit_apps`
where `approval_status` = 'Approved' and `sales_rep` = 'Joe Smith' and
`date_received` between '2014-04-30 00:00:00' AND '2014-05-15
00:00:00' and `date_ref_received` between '2014-04-27 00:00:00' AND
'2014-05-30 00:00:00')

然而,当我在MySQL Workbench或从命令行解析查询时,它解析得很好,虽然标准没有返回记录,但我知道它不会。不过,它是一个有效的查询,在MySQL中运行良好。

是什么导致Laravel抛出错误?

1 个答案:

答案 0 :(得分:1)

你应该尝试这样的事情:

select * from `credit_apps`
where `approval_status` = 'Approved' and `sales_rep` = 'Joe Smith' 
and
(`date_received` between '2014-04-30 00:00:00' and '2014-05-15 00:00:00')
and 
(`date_ref_received` between '2014-04-27 00:00:00' and '2014-05-30 00:00:00')