Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel query returns question mark instead of variable value

Tags:

php

mysql

laravel

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.

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();

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 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?

Thanks in advance!

like image 319
Kevin Kromjong Avatar asked Apr 20 '16 08:04

Kevin Kromjong


1 Answers

That's just how toSql() works. This method is used for debugging.

https://scotch.io/tutorials/debugging-queries-in-laravel

like image 81
Alexey Mezenin Avatar answered Sep 20 '22 14:09

Alexey Mezenin