Laravel Blade - 显示数组内容

时间:2016-04-25 14:44:18

标签: laravel laravel-5 laravel-blade

我调用一些API来填充数组。结束输出类似于以下

Array
(
    [0] => Array
    (
        [leadData] => Array
        (
            [LeadID] => 1232806
            [DateIdentified] => 21/04/2016
            [Client] => Prospect 1
            [LeadName] => Test
            [Owner] => Some Owner
            [Value] => 2160.00
            [Status] => 70%
        )
        [clientData] => Array
        (
            [BusinessStructure] => 
            [IsProspect] => No
        )
        [quoteData] => Array
        (
            [QuoteID] => Q0020
            [ProjectName] => Test
            [Amount] => 1800.00
            [AmountTax] => 360.00
            [AmountIncludingTax] => 2160.00
            [EstimatedCost] => 450.00
            [EstimatedCostTax] => 90.00
            [EstimatedCostIncludingTax] => 540.00
        )
        [customData] => Array
        (
            [0] => Array
            (
                [Lead Type] => New 
            )
            [1] => Array
            (
                [Month] => June
            )
        )
    )
    [1] => Array
    (
        [leadData] => Array
        (
            [LeadID] => 1230279
            [DateIdentified] => 19/04/2016
            [Client] => Bank1
            [LeadName] => test 3
            [Owner] => Some Owner
            [Value] => 36000.00
            [Status] => 50%
        )
        [clientData] => Array
        (
            [BusinessStructure] => 
            [IsProspect] => No
        )
        [quoteData] => Array
        (
            [QuoteID] => Q0016
            [ProjectName] => test 3
            [Amount] => 30000.00
            [AmountTax] => 6000.00
            [AmountIncludingTax] => 36000.00
            [EstimatedCost] => 0.00
            [EstimatedCostTax] => 0.00
            [EstimatedCostIncludingTax] => 0.00
        )
    )
)

有一点需要注意的是,有时并非所有信息都存在,只是因为它不可用。所以你可以看到Array 0有一些customData而Array 1没有。其他元素甚至可能没有quoteData部分等。

所以我的刀片模板现在有了这些信息。我基本上想要显示所有数据,但如果它不可用,只显示一个空单元格。所以我有以下标题

<table class="table table-bordered table-hover additionalMargin alignment">
    <tr class="col-md-12 noPadding">
        <thead>
        <tr>
            <th>Lead ID</th>
            <th>Client Name</th>
            <th>Is Prospect</th>
            <th>Business Structure</th>
            <th>Quote ID</th>
            <th>Project Name</th>
            <th>Amount</th>
            <th>Amount Tax</th>
            <th>Amount inc Tax</th>
            <th>Cost</th>
            <th>Cost Tax</th>
            <th>Cost inc Tax</th>
            <th>Type</th>
            <th>Month</th>
        </tr>
        </thead>
    </tr>
    <tbody>

    </tbody>
</table> 

但是,在tbody中,我正在做的事情似乎非常混乱,我能在正确的地方获得数据的唯一方法就是如果我检查所有内容,例如。

@if(is_array($leadArray))
        @foreach($leadArray as $array)
            <tr>
                 <td>
                     @if(!empty($array['leadData']))
                        {{ $array['leadData']['LeadID'] }}
                     @endif
                 </td>
                 <td>
                     @if(!empty($array['leadData']))
                        {{ $array['leadData']['Client'] }}
                     @endif
                 </td>
                 <td>
                     @if(!empty($array['clientData']))
                        {{ $array['clientData']['IsProspect'] }}
                     @endif
                 </td>
                 <td>
                     @if(!empty($array['clientData']))
                        {{ $array['clientData']['BusinessStructure'] }}
                     @endif
                 </td>
                <td>
                    @if(!empty($array['quoteData']))
                        {{ $array['quoteData']['QuoteID'] }}
                    @endif
                </td>
                <td>
                    @if(!empty($array['quoteData']))
                        {{ $array['quoteData']['ProjectName'] }}
                    @endif
                </td>
                <td>
                    @if(!empty($array['quoteData']))
                        {{ $array['quoteData']['Amount'] }}
                    @endif
                 </td>
                 <td>
                     @if(!empty($array['quoteData']))
                        {{ $array['quoteData']['AmountTax'] }}
                     @endif
                 </td>
                 <td>
                     @if(!empty($array['quoteData']))
                         {{ $array['quoteData']['AmountIncludingTax'] }}
                     @endif
                 </td>
                 <td>
                     @if(!empty($array['quoteData']))
                        {{ $array['quoteData']['EstimatedCost'] }}
                     @endif
                 </td>
                 <td>
                     @if(!empty($array['quoteData']))
                        {{ $array['quoteData']['EstimatedCostTax'] }}
                     @endif
                 </td>
                 <td>
                     @if(!empty($array['quoteData']))
                         {{ $array['quoteData']['EstimatedCostIncludingTax'] }}
                     @endif
                 </td>
                @if(!empty($array['customData']))
                    @foreach($array['customData'] as $data)
                            <td>
                                @if(!empty($data['Lead Type']))
                                    {{ $data['Lead Type'] }}
                                @endif
                            </td>
                            <td>
                                @if(!empty($data['Month']))
                                    {{ $data['Month'] }}
                                @endif
                            </td>
                    @endforeach
                    @else
                        <td></td>
                        <td></td>
                @endif
            </tr>
        @endforeach
    @endif

有更简洁的方法吗?或者这是我唯一的选择吗?

由于

2 个答案:

答案 0 :(得分:3)

Blade有一个功能,您可以使用or表示“如果它存在则回显它,如果不存在则回复”。所以你可以做到

<td>{{ $array['leadData']['LeadID'] or '' }}</td>

这基本上会产生你想要的东西。更清洁,不是吗? :)

文档:https://laravel.com/docs/5.2/blade#displaying-data - 向下几段。

答案 1 :(得分:1)

通过使用列名称迭代数组(使用or ''作为Joel建议),您也可以更优雅,更短地完成此操作:

{{ $dataColumns['leadData'] = ['LeadID', 'Client'] }}
{{ $dataColumns['clientData'] = ['IsProspect', 'BusinessStructure'] }}
{{ #dataColumns['quoteData'] = ['QuoteID', 'ProjectName', 'Amount', 'AmountTax', 'AmountIncludingTax', 'EstimatedCost', 'EstimatedCostTax', 'EstimatedCostIncludingTax'] }}

@if(is_array($leadArray))
    @foreach($leadArray as $array)
        <tr>
            @foreach($dataColumns as $dataColumn)
                @foreach($dataColumn as $column)
                    <td>{{ $array[$dataColumn][$column] or '' }}</td>
                @endforeach
            @endforeach
            @if(!empty($array['customData']))
                @foreach($array['customData'] as $data)
                    <td>{{ $data['Lead Type'] or ''}}</td>
                    <td>{{ $data['Month'] or '' }}</td>
                @endforeach
            @else
                    <td></td>
                    <td></td>
            @endif
        </tr>
    @endforeach
@endif