Support the ongoing development of Laravel.io →
posted 9 years ago
Database
Last updated 2 years ago.
0

Try this.


$id_list = [5,9,13]; 

$data_list = DB::table('table') ->whereIn('id', $id_list)->get();

0

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?

0

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]);
Last updated 9 years ago.
0

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.

0

Thanks.

0

Sign in to participate in this thread!

Eventy

Your banner here too?

Cholisky cholisky Joined 20 Feb 2015

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.