Sending arrays to Blade component (Laravel 8)

  laravel, laravel-8, laravel-blade, php

I am attempting to create a x-select component where I can pass an array of options as one of the attributes.

Here is create.blade.php:

<?php $list = ['Received', 'In Process', 'Repaired', 'Completed', 'Shipped', 'Waiting on Boards', 'In Route'];?>
<x-select id="progress" class="block mt-1 w-full" name="progress" :value="old('progress')" :list="$list" />

Here is the component select.blade.php

@props(['disabled' => false])

<select {{ $disabled ? 'disabled' : '' }} {!! $attributes->merge(['class' => 'rounded-md shadow-sm border-gray-300 focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50']) !!}>
    @foreach ($list as $item)
        <option @if($item == $value) selected @endif>$item</option>

The problem is that it’s attempting to parse/print the :list when it runs the $attributes->merge(). This doesn’t seem like it should be the case, as according to the Laravel 8 Blade docs it appears you can add an :attribute that doesn’t get printed when running $attributes->merge(). See:

If I change the value of :list to a normal string it will print the string when running $attributes->merge() which is contrary to those docs and makes it so that I cannot pass an array because it attempts to trim the array and fails with this error:

trim() expects parameter 1 to be string, array given

Source: Laravel

Leave a Reply