简体   繁体   中英

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

My parent view looks like this:

@include('inquiries.partials.inquiries')

It uses the following partial:

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

Which uses another partial:

<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 I want to define section for , however I need to access variable from 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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM