Laravel Component Pass Array

To pass an array to a Laravel component, you can use the $attributes property. This property allows you to pass data from the parent component to the child component. Here’s an example:

Parent Component:

    
      <x-child-component :items="['item1', 'item2', 'item3']" />
    
  

Child Component (x-child-component.blade.php):

    
      <div>
        <ul>
          @foreach ($items as $item)
            <li>{{ $item }}</li>
          @endforeach
        </ul>
      </div>

      ...

      public $items;

      public function mount($items)
      {
        $this->items = $items;
      }
    
  

In the parent component, you can pass the array of items to the child component using the :items syntax. In the child component, you can access the items through the $items property. The mount method is used to initialize the value of $items.

You can then loop through the $items variable in the child component’s blade file to display each item.

Leave a comment