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

rubendn's avatar

Pass Collection or Array to Blade Component

Is it possible to pass a collection or key/value pair array into either an Anonymous Blade component or Blade component with a class?

I want to create a select blade component that and I'm trying to pass in the options.

I've tried the following and it does not work:

Is it possible to pass a collection or key/value pair array into either an Anonymous Blade component or Blade component with a class?

I want to create a select blade component that and I'm trying to pass in the options.

I've tried the following and it does not work:


<!-- resources\views\components\select.blade.php -->

<div>
    <select>
        @foreach($list as $key => $value)
            <option value="{{$key}}">{{$value}}</option>
        @endforeach
    </select>
</div>

<x-select :list="$list" />
0 likes
29 replies
Sinnbeck's avatar

Can you try posting the code again (you can just edit your question)

rubendn's avatar

Updated code above.

Error that I am getting on the page using the component is:

syntax error, unexpected '<'

Sinnbeck's avatar

You are missing an opening < in option (well it is in the wrong place really)

<option value="{{$key}}">{{$value}}</option>
rubendn's avatar

Yes, I saw that and fixed it and it still giving the same error.

laracoft's avatar

clear your cache, php artisan view:clear

1 like
rubendn's avatar

Cleared cache after you mentioned it and still getting the same error.

This is the line that the exception is occurring at:

<?php $component = $__env->getContainer()->make(Illuminate\View\AnonymousComponent::class, ['view' => 'components.select','data' => ['list' => <?php echo e($list); ?>]]); ?>
rubendn's avatar

This is the value of $list from a dd.

Illuminate\Support\Collection {#1588 ▼
  #items: array:4 [▼
    0 => "TS"
    1 => "US"
    2 => "GS"
    3 => "PL"
  ]
}
laracoft's avatar

Can you paste the syntax error in full? It doesn't sound like Chrome, where are you seeing it? If it is a Chrome error, show HTML surrounding the error.

laracoft's avatar

You have a nested <?php on line 83. Show full bulk-edit.blade.php

laracoft's avatar

Do you know who/what is creating this line?

<?php $component = $__env->getContainer()->make(Illuminate\View\AnonymousComponent::class, ['view' => 'components.select','data' => ['list' => <?php echo e($list); ?>]]); ?>

The 2nd <?php tag cannot be inside the 1st <?php tag. The 1st tag has to close first before the 2nd can open.

rubendn's avatar

This is the section in bulk-edit.blade.php where the blade component is inserted...

              @foreach($items as $item)
                    <tr class="bg-white">
                        <td class="px-1 py-1 whitespace-no-wrap text-sm leading-5 font-medium text-gray-900 text-center">
                            {{$loop->iteration}}
                        </td>
                        <td class="px-1 py-1 whitespace-no-wrap text-sm leading-5 text-gray-500">
                            <input name="ItemYear" value="{{$item->ItemYear}}" type="text" class="bg-gray-200 appearance-none border-2 border-gray-300 text-xs  py-1 px-1 text-gray-800 leading-tight focus:outline-none focus:bg-white focus:border-blue-500">
                          </td>                        
                        <td class="px-1 py-1 whitespace-no-wrap text-sm leading-5 text-gray-500">

                        </td>
                        <td class="px-1 py-1 whitespace-no-wrap text-sm leading-5 text-gray-500">
                            {{$item->PlayerFirst}} {{$item->PlayerLast}} {{$item->PlayerSuffix}}
                        </td>
                        <td class="px-1 py-1 whitespace-no-wrap text-sm leading-5 text-gray-500">
                            <x-select :list="{{$list}}"/>
                        </td>
                        <td class="px-1 py-1 whitespace-no-wrap text-sm leading-5 text-gray-500">
                            {{$item->ItemYear}}
                        </td>
                        <td class="px-1 py-1 whitespace-no-wrap text-sm leading-5 text-gray-500">
                            {{$item->ItemYear}}
                        </td>
                    </tr>
              @endforeach
rubendn's avatar

If I remove

<x-select :list="{{$list}}"/>

from the above code. The page runs fine.

laracoft's avatar
laracoft
Best Answer
Level 27

Use <x-select :list="$list"/>

9 likes
rubendn's avatar

Switching it to that now gives the following error:

ErrorException Undefined variable: list (View: C:\laragon\www\fcs\resources\views\admin\bulk-edit.blade.php)

laracoft's avatar

Show the method calling view("admin.bulk-edit")

rubendn's avatar

This is the method in the controller. I dumped out the $list variable to test and there is data in it like shown above before the view is called.

return view('admin.bulk-edit',['items' => $items, 'itemTypes' => $itemTypes, '$list' => $list]);

I tried changing the blade component to the following by adding the props and still the same error of undefined variable

@props(['list'])
<div>
    <select>
        @foreach($list as $key => $value)
            <option value="{{$key}}">{{$value}}</option>
        @endforeach
    </select>
</div>
newbie360's avatar

seems you are direct render in controller without via component class ?

can you show the component class code

laracoft's avatar

Can you replace <x-select :list="$list" /> with this?

@component('select', ['list' => $list]) @endcomponent
rubendn's avatar

Tried replacing and it still shows the same error.

Are we sure that a blade component can accept an array/collection?

1 like
rubendn's avatar

newbie360: I'm not sure I understand your questions but are you asking if there is an associated class with this blade component? If so, there is not.

laracoft's avatar

@rubendn I mean, a variable is just a variable. Otherwise, how do we get something like this to work? @foreach($list as $key => $value)

laracoft's avatar

Which line is this complaining about ErrorException Undefined variable: list (View: C:\laragon\www\fcs\resources\views\admin\bulk-edit.blade.php)

rubendn's avatar

This line:

<?php $__env->startComponent('select', ['list' => $list]); ?> <?php if (isset($__componentOriginal81448fe273247b533b9f018e96c158cab7901247)): ?>

in:

Illuminate\View\Engines\CompilerEngine::handleViewException C:\laragon\www\fcs\storage\framework\views\f8191669a467f441da920a1dd1b6b03debc22b11.php:83

laracoft's avatar

OMG. fix this:

return view('admin.bulk-edit',['items' => $items, 'itemTypes' => $itemTypes, 'list' => $list]);
rubendn's avatar

LOL. The extra $ caused this.

Seems to run fine now.

Thanks for all your help.

VaughnKennett's avatar
 <x-select :list="$list" />

is correct - the list is passed.

@php $list = [1 => 'a',2 => 'b',3 => 'c'] @endphp
    <x-select :list="$list" />

where x-select =

@foreach($list as $key => $value)
            {{$key}} - {{$value}}
        @endforeach

produces

1 - a 2 - b 3 - c

Just saying!

2 likes
cibermesias's avatar

Same problem here. I just forgot to add $list var to the __construct method of the component.

Please or to participate in this conversation.