0

I have laravel eloquent example query:

DB::select('SELECT * FROM users u WHERE u.id IN :param', ['param' => [1, 2, 3]]);

But it doesn't work. How can I bind this array parameter to query.

In symfony I could use ->setParameter('param', Connection::PARAM_INT_ARRAY); But I dont see option do it the same in Laravel.

Regards.

Klick
  • 1,418
  • 1
  • 23
  • 45
  • https://stackoverflow.com/questions/29115385/how-to-make-laravel-eloquent-in-query maybe this will help you – Cositanto Jul 28 '22 at 06:30

3 Answers3

3

otherway, You can use a string and a Mark selector insteade of param :

DB::select('SELECT * FROM users u WHERE u.id IN (?)', ['1, 2, 3']);
Romylussone
  • 773
  • 1
  • 8
  • 19
0

use eloquent whereIn to find the users in the params array.

$params = [1,2,3]
User::whereIn("id", $params)->get();
Robert S
  • 626
  • 6
  • 13
  • for DB query you can use $params = [1,2,3] $users = DB::table('users')->whereIn('id', $params)->get(); – Robert S Jul 28 '22 at 06:36
-1

If you are using eloquent than you can do this ...

$id = [1,2,3];
$data = Model::find($id);

reference here

if you are using db query than you can do like this way ...

$data = DB::table('users')
                ->whereIn('id', [1, 2, 3])
                ->get();

reference here

may it helps you ...

  • As I wrote it was only example query. My real query is much more complicate and I can't use anything related to model. – Klick Jul 28 '22 at 06:48