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

dk4210's avatar

Laravel - query table array of ids

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.

0 likes
7 replies
tykus's avatar
tykus
Best Answer
Level 104

Use whereIn when you have an array of data to constrain the query:

$get_header_keys = DB::table('payload')->whereIn('id', $headerpayload)->get();
dk4210's avatar

Perfect. I thought I tried that. Thanks!

dk4210's avatar

Hey @tykus ,

I would I get the value for "key_value" for each one?

tykus's avatar

"key_value" for each one

For each what?

dk4210's avatar

I am returning 5 result sets like this one

 +"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

How would I get the "Key_value" value?

I tried this

echo $get_header_keys->key_value;

But sadly, that doesn't work.

I get this

Property [key_value] does not exist on this collection instance.
MichalOravec's avatar

Do you have a problem to understand the error message?

Property [key_value] does not exist on this collection instance.

It's pretty clear...

dk4210's avatar

Collections vs Arrays vs objects still confuse me.

Please or to participate in this conversation.