Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading