How to use WHERE NOT IN in DB:raw Laravel?
Here’s my sample code
$query = DB::select(DB::raw("... WHERE ID NOT IN (:ids) ..", array( 'ids' => "1,2,3" )));
I also tried this
$ids = "'1','2','3'";
$query = DB::select(DB::raw("... WHERE ID NOT IN (:ids) ..", array( 'ids' => $ids )));
3
Answers
Got it. Thank you for your answers, but I use this solution:
You would need to do use
setBindings
to bind your params like belowAnswer based on docs https://laravel.com/api/10.x/Illuminate/Database/Query/Builder.html#method_setBindings
I suppose the error, "Unknown column ‘ids’ in ‘where clause’", suggests that the database engine is treating ‘ids’ as a column name rather than as a parameter placeholder.
The below change can probably work: