Laravel查询返回问号而不是变量值 [英] Laravel query returns question mark instead of variable value
问题描述
当我单击一个按钮时,正在对我自己编写的API进行AJAX调用.单击该按钮时,Javascript文件从单击的对象中检索两个 data
属性,并将其与URL一起发送.这两个变量在尚未执行的SQL语句的where子句中使用.
When I click on a button, an AJAX-call is being made to my self written API. When the button is clicked, the Javascript file retrieves two data
-attributes from the clicked object and sends it along with the URL. The two variables are used in the where clause of the yet to be executed SQL-statement.
进行调用时,处理API请求的PHP脚本通过 Input :: get('data-attribute-one'))
和 Input接收两个变量:: get('data-attribute-two'))
.当我输出这两个属性时,将显示正确的值.
When the call is being made, the PHP-script that handles the API requests receives the two variables via Input::get('data-attribute-one'))
and Input::get('data-attribute-two'))
. When I output these two attributes, the correct values are displaying.
但是,当我将变量放入查询的子句时,就像这样:
However, when I put the variables into the where-clause of my query, like so:
$fans = Sensor::where('fan_number', '=', $dataAttributeOne)->where('created_at', '>=', $this->now->subHours(6))->get();
没有返回结果.因此,我用-> toSql()
替换了-> get()
来检查查询,并向我展示了以下奇怪的代码行:
no results are returned. So I replaced ->get()
with ->toSql()
to check the query and it presented me with this strange line of code:
Object {fans: "select * from "myawesomedatabasetable" where "fan_number" = ? and "created_at" >= ?"}
$ dataAttributeOne
是检索到的 data-attribute
,而 $ this-> now-> subHours(6)
是当前时间减去6小时.当我输出它时,它的格式与数据库中 created_at
值的格式相同,当我在Phpmyadmin中将其硬编码为SQL语句时,查询的执行就像一个超级按钮.因此那里没有问题.
$dataAttributeOne
is the retrieved data-atribute
and $this->now->subHours(6)
is the current time minus 6 hours. When I output this, it is in the same format as the created_at
value in the database and when I hardcode it into an SQL-statement in Phpmyadmin, the query executes like a charm. So no problems there.
所以我的问题是:为什么执行查询时变量会变成问号,我该如何解决?
So my question is: why are the variables turning into question marks when the query executes and how do I solve this?
提前谢谢!
推荐答案
这就是 toSql()
的工作方式.此方法用于调试.
That's just how toSql()
works. This method is used for debugging.
https://scotch.io/tutorials/debugging-queries-in-laravel
这篇关于Laravel查询返回问号而不是变量值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!