How to pass an array variable in a laravel component?

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

How to pass an array variable in a laravel component?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by orpha , 9 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.

Related Threads:

How to pass laravel env data to vue.js component?
How to pass variable from guzzle request in laravel?
How to pass a variable in join query in laravel?
How to pass a string or variable with http redirect in laravel?
How to pass array to trait in laravel?
How to pass a variable from python 3 to php?