简体   繁体   English

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

[英]Query fails in Laravel but works in MySQL

I have some query scope methods defined in an Eloquent model and am calling them in the controller to allow Laravel to build the query using 我在Eloquent模型中定义了一些查询范围方法,并在控制器中调用它们,以允许Laravel使用以下命令构建查询

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

The error returned by Illuminate is a MySQL syntax error: 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')

However when I parse the query in MySQL Workbench or from the command line it parses just fine although the criteria returns no records, but I knew it wouldn't. 但是,当我在MySQL Workbench中或从命令行解析查询时,尽管条件不返回任何记录,但它解析得很好,但是我知道不会。 Nevertheless, it's a valid query and works fine in MySQL. 不过,这是一个有效的查询,并且在MySQL中可以正常工作。

What is causing Laravel to throw the error? 是什么导致Laravel抛出错误?

You should try something like this: 您应该尝试这样的事情:

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')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM