Use whereIn when you have an array of data to constrain the query:
$get_header_keys = DB::table('payload')->whereIn('id', $headerpayload)->get();
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hello guys,
I'm trying to query a table with an array of ids, but for some reason It will only show one.
I have the following
$headerpayload = DB::table('netsecure_payload')->pluck('payload_id')->all();
Which outputs the following
array:5 [▼
0 => 1
1 => 7
2 => 8
3 => 24
4 => 25
]
I need all these values to query the next table for all the data based on these ids
Here is the next query
$get_header_keys = DB::table('payload')->where('id', '=', $payloadkey)->get();
It outputs just one.
Illuminate\Support\Collection {#1716 ▼
#items: array:1 [▼
0 => {#1724 ▼
+"id": 1
+"type": 1
+"name": "Xcalling ID"
+"key_value": "X-CALLINGAPPID"
+"status": 1
+"value_type": 1
+"static_value": "myvalue"
+"dynamic_value": null
+"created_at": null
+"updated_at": null
}
]
}
I also tried to add in a foreach loop, but I get the same thing. Strange. Any idea what I'm doing wrong?
foreach($headerpayload as $payloadkey){
$get_header_keys = DB::table('payload')->where('id', '=', $payloadkey)->get();
dd($get_header_keys);
}
Thanks in advance.
Use whereIn when you have an array of data to constrain the query:
$get_header_keys = DB::table('payload')->whereIn('id', $headerpayload)->get();
Please or to participate in this conversation.