Can you try posting the code again (you can just edit your question)
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" />
Can't see what you have tried
Updated code above.
Error that I am getting on the page using the component is:
syntax error, unexpected '<'
You are missing an opening < in option (well it is in the wrong place really)
<option value="{{$key}}">{{$value}}</option>
Yes, I saw that and fixed it and it still giving the same error.
clear your cache, php artisan view:clear
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); ?>]]); ?>
This is the value of $list from a dd.
Illuminate\Support\Collection {#1588 ▼
#items: array:4 [▼
0 => "TS"
1 => "US"
2 => "GS"
3 => "PL"
]
}
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.
You have a nested <?php on line 83. Show full bulk-edit.blade.php
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.
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
If I remove
<x-select :list="{{$list}}"/>
from the above code. The page runs fine.
Use <x-select :list="$list"/>
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)
Show the method calling view("admin.bulk-edit")
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>
seems you are direct render in controller without via component class ?
can you show the component class code
Can you replace <x-select :list="$list" /> with this?
@component('select', ['list' => $list]) @endcomponent
Tried replacing and it still shows the same error.
Are we sure that a blade component can accept an array/collection?
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.
@rubendn can't pass via slot but via the array method I showed. https://stackoverflow.com/questions/41909142/how-to-pass-an-array-to-a-slot-in-laravel-5-4/49136223
Which line is this complaining about ErrorException Undefined variable: list (View: C:\laragon\www\fcs\resources\views\admin\bulk-edit.blade.php)
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
OMG. fix this:
return view('admin.bulk-edit',['items' => $items, 'itemTypes' => $itemTypes, 'list' => $list]);
LOL. The extra $ caused this.
Seems to run fine now.
Thanks for all your help.
<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!
Same problem here. I just forgot to add $list var to the __construct method of the component.
Please or to participate in this conversation.