Laravel: Pass a variable to a tab without using Javascript

时间:2019-05-31 11:24:26

标签: php laravel laravel-blade

I am building an index page in whicgh I show the orders separated in three tables: "incoming", "in_progress", "ready"

Essencially is this same table shown three times (and separated into a different tab)

<table class="table table-striped table-hover dataTables-example">
    <thead class="thead-dark">
        <tr>
            <th>Id</th>
            <th>Eingang</th>
            <th>Auftrag id</th>
            <th>Ort</th>
        </tr>
    </thead>
    <tbody>
        @foreach($all_ as $element)
        @if($element->status === 'in_progress')
        <tr>
            <td>{{$element->id}}</td>
            <td class="gradeX">{!! $element->created_at->format('D j M Y, G:i:s') !!}</td>
            <td><a href="{{route('orders.show', $element->title)}}">{!! $element->title !!}</a></td>
            <td>{!! $element->address->plz->locality !!}</td>
        </tr>
        @endif
        @endforeach
    </tbody>
</table>

I saved the table in a different file and I called it like this: @include('dashboard.orders.includes.table_orders')

....
<div class="tab-content py-3 px-3 px-sm-0" id="nav-tabContent">
    <div class="tab-pane fade show active" id="nav-home" role="tabpanel" aria-labelledby="nav-home-tab">
        <!-- I need to call here the partial and pass status="incoming" -->
        @include('dashboard.orders.includes.table_orders')
    </div>

    <div class="tab-pane fade" id="nav-profile" role="tabpanel" aria-labelledby="nav-profile-tab">
        <!-- I need to call here the partial and pass status="in progress" -->
        @include('dashboard.orders.includes.table_orders')
    </div>

    <div class="tab-pane fade" id="nav-contact" role="tabpanel" aria-labelledby="nav-contact-tab">
        <!-- I need to call here the partial and pass status="ready" -->
        @include('dashboard.orders.includes.table_orders')
    </div>
</div>

The model looks lilke this:

$element [
    "id" => 1
    "title" => "KFZ_0001_May_2019"
    "details" => "Lorem Ipsums dolor..."
    "status" => "in progress"    // <---or incoming or ready
    "created_at" => "2019-05-31 09:14:02"
    "updated_at" => "2019-05-31 11:16:55"
    "deleted_at" => null
  ]

The question is:

How do I call the external partial and pass the "status" variable in order to show the orders separated each in its corresponding table?

All the three tables are show in the same page!

I would like to avoid using of javascript just for that.

3 个答案:

答案 0 :(得分:1)

据我所知,我会做这样的事情:

@include('dashboard.orders.includes.table_orders', ['status' => 'incoming'])

然后

@foreach($all_ as $element)
    @if($element->status === $status)
        ...
    @endif
@endforeach

如果要输出三次数据,而只输出某些变量之间的差异,则不会违反DRY原理。

答案 1 :(得分:1)

您可以通过将数组作为第二个参数传递来将变量传递给包含的刀片文件:

<div class="tab-content py-3 px-3 px-sm-0" id="nav-tabContent">
    <div class="tab-pane fade show active" id="nav-home" role="tabpanel" aria-labelledby="nav-home-tab">
        @include('dashboard.orders.includes.table_orders', ['status' => 'incoming'])
    </div>

    <div class="tab-pane fade" id="nav-profile" role="tabpanel" aria-labelledby="nav-profile-tab">
        @include('dashboard.orders.includes.table_orders', ['status' => 'in_progress'])
    </div>

    <div class="tab-pane fade" id="nav-contact" role="tabpanel" aria-labelledby="nav-contact-tab">
        @include('dashboard.orders.includes.table_orders', ['status' => 'ready'])
    </div>
</div>

并在您的table_orders模板中:

@foreach($all_ as $element)
    @if($element->status === $status)
    <tr>
        {{ .. your row format }}
    </tr>
    @endif
@endforeach

如果您想进一步泛化代码,而无需重复3次相同的包含并轻松添加不同的状态,则可以将如下对象传递给视图:

$tabs = [
  ['status' => 'incoming', 'id' => 'nav-home', 'label' => 'nav-home-tab'],
  ['status' => 'in_progress', 'id' => 'nav-profile', 'label' => 'nav-profile-tab'],
  ['status' => 'ready', 'id' => 'nav-contact', 'label' => 'nav-contact-tab'],
];

并在您看来:

@foreach($tabs as $tab)
    <div class="tab-pane fade show active" id="{{ $tab['id'] }}" role="tabpanel" aria-labelledby="{{ $tab['label'] }}">
        @include('dashboard.orders.includes.table_orders', ['status' => $tab['status']])
    </div>
@endforeach

答案 2 :(得分:0)

Try doing it like this @include('partial_name', $element)

Then you can access the variable as the name you passed to the include