Search code examples
phplaravelcomponentslaravel-blade

How can I pass an array variable in a Laravel component?


I want to pass array variable to my component.

Here is my code:

@php
    $breadcrumbs = [
    [
        'name' => 'Trainees',
        'link' => route('trainee.list')
    ]
];
@endphp

@section('breadcrumbs')
    <x-breadcrumbs/>
@endsection

When I try to access $breadcrumbs it says undefined variable.


Solution

  • You can pass an array to your component like this,

    <x-breadcrumbs :breadcrumbs="$breadcrumbs"/>
    

    If you want to pass simple string data,

    <x-breadcrumbs value="Example String"/>
    

    you have to define these variables in the component's constructor except if it is an anonymous component.

    you can check doc for more detailed info : https://laravel.com/docs/10.x/blade#passing-data-to-components