0

I am using below query to get results of 4 id's, but it shows only one result

$id = array(6,7,8,9);      
        $params = array(
            'connection' => 'redis',
            'id' => implode(',',$id)
        );

        $result =  DB::select(
            DB::raw("
                SELECT 
                    * 
                FROM 
                    failed_jobs 
                WHERE 
                    id IN (:id) AND 
                    connection = :connection
            "), $params);
vignesh.D
  • 766
  • 4
  • 18
  • 1
    manual binding for an array should be like `"WHERE IN (:id1, :id2, :id3)"` with the params `['id1'=>6, 'id2'=>7, 'id3'=>8]` which you have to work around to handle the generation of the prepared statement – 0nepeop1e Feb 09 '22 at 14:20

1 Answers1

2

The best way to go about it is to use the query builder instead. This is much cleaner since it provides the whereIn clause:

$id = array(6,7,8,9);

$result = DB::table('failed_jobs')
    ->where('connection', 'redis')
    ->whereIn('id', $id)
    ->get();
aynber
  • 22,380
  • 8
  • 50
  • 63
  • Cant we achieve result in my query using colon – vignesh.D Feb 09 '22 at 14:09
  • Need to know why its showing only one result – vignesh.D Feb 09 '22 at 14:11
  • 1
    Not really, because of the `in` clause. Question marks would be easier, but you cannot implode your array. https://stackoverflow.com/questions/920353/can-i-bind-an-array-to-an-in-condition has more information on that. – aynber Feb 09 '22 at 14:11