Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

pordonez's avatar

How to convert laravel query to db::raw

Hi, i have this query

$array = ["1","2","3"];
Leads::whereIn('lead_agent', $array);

i want to convert this query to a simple db::raw like this

DB::select('select * from leads where???');

i dont know how to pass the $array into the simple db::raw query

0 likes
3 replies
tylernathanreed's avatar

The second parameter to DB::select(...) is your bindings. This means that you can do this:

DB::select('select * from leads where lead_agent in ?', [$array]);
pordonez's avatar

@tylernathanreed can i do something like this?

DB::select('select * from leads where lead_agent in ?', [$array].' order by created_at desc');
tylernathanreed's avatar

@pordonez

No, because the bindings escapes SQL to prevent it from changing the code. You're also trying to concatenate an array to a string.

You're better off doing this:

DB::table('leads')->whereIn('lead_agent', $array)->orderBy('created_at', 'desc');

Please or to participate in this conversation.