Try this.
$id_list = [5,9,13];
$data_list = DB::table('table') ->whereIn('id', $id_list)->get();
What I posted there is just the key thing not working in a very large SQL call. Is there a way to modify it 'as is' without rewriting the whole thing?
I think Ron's answer is what you are looking for. You want to use the IN clause instead of equals. His answer just uses more of the query builder tools. If you don't want to use them, your code could look more like the following.
// Your array of ids
$id_list = [5,9,13];
// Make your array of ids into a comma separated string
$id_str = implode(',', $id_list);
// Pass that string into your IN clause in the query
DB::select('SELECT * FROM table WHERE table.id IN (?)', [$id_str]);
Actually what you posted won't work because of the binding. It will construct a query that looks like
SELECT * FROM table WHERE table.id IN ('5,9,13')
which is not what you want. It's better to just refactor it to use the query builder. It's not that hard.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community