Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 750
Mike Avatar asked Sep 29 '22 08:09

Mike


1 Answers

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.

like image 106
Ravan Scafi Avatar answered Oct 02 '22 13:10

Ravan Scafi