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

david19's avatar

Passing Array to PHP Component

I call the component from my controller here:

Blade::renderComponent(new Card('Benutzer','Alle Benutzer',[$this->tableColumns()]));

You can see, the 3rd Argument is a array.

<?php

namespace Hippoz\Datatable\View\Components;

use Illuminate\View\Component;

class Card extends Component
{
 
    public $header;
    public $cardHeader;
    public $tableColumns;


    public function __construct(string $header, string $cardHeader,array $tableColumns)
    {
        $this->header       = $header;
        $this->cardHeader   = $cardHeader;
        $this->tableHeaders = $tableHeaders;
        $this->tableColumns = $tableColumns;
      
        
        

    }

    /**
     * Get the view / contents that represent the component.
     *
     * @return \Illuminate\Contracts\View\View|\Closure|string
     */
    public function render()
    {
        return view('hippoz::components.card');
    }
}

I can not pass the array $tableColumns. I get allways,

Array to string conversion
0 likes
1 reply
tykus's avatar
tykus
Best Answer
Level 104

Show how you're passing the Array; do you use the : prefix on the attribute name:

<x-card header="Your Header" card-header="Card Header" :table-columns="['column_name']" />

PHP expressions and variables should be passed to the component via attributes that use the : character as a prefix:

https://laravel.com/docs/9.x/blade#passing-data-to-components

Please or to participate in this conversation.