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

triadi's avatar
Level 1

Issue with Array Reindexing in Livewire Component Passed to Blade View

Hi everyone, I’m facing an issue with array reindexing in a Laravel Livewire component. Here's the situation:

In my Livewire component, I prepare an array with custom keys (IDs) and values (name). The array looks like this when dumped directly in the Livewire component:

array:15 [
  9 => "Yayasan Sanbe Farma"
  20 => "Tes New Mitra 11"
  11 => "Penerbit Erlangga"
  2 => "Partner Petra Kovacek"
  3 => "Partner Mr. Ulices Haley"
  1 => "Partner Alexandro Nienow"
  5 => "Mitra ABC"
  13 => "Mitra 1"
  19 => "LPPM ITB"
  8 => "KEMENDIKBUDRISTEK"
  10 => "Institut Teknologi Bandung"
  21 => "halo testing logi"
  22 => "halo aku baru testing nih"
  12 => "Bank Negara Indonesia (BNI)"
  6 => "Asrama"
]

But when I pass this array to the Blade view and dump it using @dd($options['partner']), the array gets reindexed starting from 1:

array:15 [
  1 => "Partner Alexandro Nienow"
  2 => "Partner Petra Kovacek"
  3 => "Partner Mr. Ulices Haley"
  5 => "Mitra ABC"
  6 => "Asrama"
  8 => "KEMENDIKBUDRISTEK"
  9 => "Yayasan Sanbe Farma"
  10 => "Institut Teknologi Bandung"
  11 => "Penerbit Erlangga"
  12 => "Bank Negara Indonesia (BNI)"
  13 => "Mitra 1"
  19 => "LPPM ITB"
  20 => "Tes New Mitra 11"
  21 => "halo testing logi"
  22 => "halo aku baru testing nih"
]

I need the array to keep its original keys in the Blade view. Here's how I set the data in the Livewire component:

$this->options['partner'] = Partner::getDropdownOptionsStatic('name', Partner::query()->orderBy('name', 'desc'));

Question: How can I ensure the array retains its original keys when passed from a Livewire component to a Blade view? Is there a specific way to handle this in Livewire or Blade to prevent automatic reindexing?

Thanks in advance!

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

I suppose a Javascript representation of the array is corrupting the orderr of the keys. I would suggest that you created an array of hashes to preserve the ordering, e.g.

[
  ["id'" => 9, "name" => "Yayasan Sanbe Farma"],
  ["id" => 20, "name" => "Tes New Mitra 11"],
  // ...
  ["id" => 6, "name" => "Asrama"],
]
1 like

Please or to participate in this conversation.