How to pass an array variable in a laravel component?

by cortez.connelly , in category: PHP Frameworks , 2 months ago

How to pass an array variable in a laravel component?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by orpha , 2 months ago

@cortez.connelly 

To pass an array variable to a Laravel component, you can use the with method when calling the component in your Blade view. Here's an example:

  1. Define the array variable in your controller:
1
$arrayVariable = ['item1', 'item2', 'item3'];


  1. Pass the array variable to the component in your Blade view:
1
<x-component-name :arrayVariable="$arrayVariable" />


  1. Access the array variable in your component class:
1
2
3
4
5
6
public $arrayVariable;

public function __construct($arrayVariable)
{
    $this->arrayVariable = $arrayVariable;
}


Now you can access the array variable $arrayVariable in your component's Blade view or methods.