[英]How to insert SQL column name to Raw Query from PHP/Laravel
I have this code in Laravel:我在 Laravel 中有这个代码:
DB::table('items')
->whereRaw("? = 1", ['active'])
->get();
In my database table, I have a column named active
and the query I want to run is:在我的数据库表中,我有一个名为active
的列,我要运行的查询是:
SELECT *
FROM items
WHERE active=1
My code fails because the query passes my 'active'
parameter as a String instead of a column name in SQL syntax (which is the expected behavior).我的代码失败,因为查询将我'active'
参数作为字符串传递,而不是 SQL 语法中的列名(这是预期的行为)。 So, instead of the above, I get something like this:因此,我得到的不是上面的,而是这样的:
SELECT *
FROM items
WHERE "active"=1
Any idea how to solve this?知道如何解决这个问题吗?
PS: I tried the MySQL function TRIM but with no success (perhaps I did not do it correctly). PS:我尝试了 MySQL function TRIM 但没有成功(也许我没有正确执行)。
It is not the cleanest way;这不是最干净的方法;
$day = 'Monday'; // dynamically Tuesday, Wednesday....
$method = 'where' . $day;
return DB::table('items')->$method('1')->get();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.