padawanTony
padawanTony

Reputation: 1359

How to insert SQL column name to Raw Query from PHP/Laravel

I have this code in 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:

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

Upvotes: 0

Views: 429

Answers (1)

Ersoy
Ersoy

Reputation: 9594

It is not the cleanest way;

$day = 'Monday'; // dynamically Tuesday, Wednesday....
$method = 'where' . $day;

return DB::table('items')->$method('1')->get();

Upvotes: 1

Related Questions