Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass an array to a @slot in laravel 5.4?

I am trying to use it in my layout and I'm suddenly bump-up with the idea of using the new features of Laravel 5.4 @slot.

Just wondering if passing an array to a slot possible?

        @section('SampleSection')

            @component( 'mylayouts.partials.contentheader' )

                @slot('title')
                    Sample Title
                @endslot

                @slot('indexes')
                    Pass array here  // example [ 'a', 'b', 'c' ]
                @endslot

            @endcomponent

        @endsection
like image 651
Nelson Melecio Avatar asked Jan 28 '17 11:01

Nelson Melecio


1 Answers

As far as I know, it isn't possible to pass an array to a component via @slot. However, it is possible to pass and an array to a component directly, without using a slot.

From the documentation:

Passing Additional Data To Components

Sometimes you may need to pass additional data to a component. For this reason, you can pass an array of data as the second argument to the @component directive. All of the data will be made available to the component template as variables:

@component('alert', ['foo' => 'bar'])
    ...
@endcomponent

So let's say you have an array of data:

$my_array = ['a', 'b', 'c']

You can pass it to the component like so:

@component('mylayouts.partials.contentheader', ['my_array' => $my_array])
    ...
@endcomponent

Or you can just pass it directly, without using a variable:

@component('mylayouts.partials.contentheader', ['my_array' => ['a', 'b', 'c']])
    ...
@endcomponent

Then, within your component file, you will have access to a $my_array variable. For example, in your contentheader.blade.php file you can do this:

<ul>
    @foreach($my_array as $value)
        <li>{{ $value }}</li>
    @endforeach
</ul>
like image 169
idix Avatar answered Sep 24 '22 14:09

idix