Mike
Mike

Reputation: 367

Laravel Blade: Getting access to a variable in a nested partial from a parent view

My parent view looks like this:
show.blade.php

@include('inquiries.partials.inquiries')

It uses the following partial:
inquiries.blade.php

<ul>
    @foreach($inquiry as $key => $item)
        <li>
            @include('inquiries.partials.inquiry')
        </li>
    @endforeach
</ul>

Which uses another partial:
inquiry.blade.php

<div class="row">
    <div class="col-xs-10">
        <div class="data"> ... </div>
    </div>
    <div class="col-xs-2 text-right">
         @yield('inquiry.toolbar', '')
    </div>
</div>

In show.blade.php I want to define inquiry.toolbar section for inquiry.blade.php, however I need to access $key variable from inquiries.blade.php file, like so:

@include('inquiries.partials.inquiries')

@section('inquiry.toolbar')
{!! button_delete([
    'route' => ['inquiries.items.destroy', $key]
]) !!}
@stop

However, the code above does not work (I get "Undefined variable: key").
Is it possible?

Upvotes: 6

Views: 6337

Answers (1)

Ravan Scafi
Ravan Scafi

Reputation: 6392

You can pass data when including a view, like so:

<ul>
    @foreach($inquiry as $key => $item)
        <li>
            @include('inquiries.partials.inquiry', compact('key'))
        </li>
    @endforeach
</ul>

And it will be available on your view.

Check the docs for more information.

Upvotes: 4

Related Questions