0

I make calls to some APIs to fill up an array. The end output is something like the following

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
        )
    )
)

One thing to note is that sometimes not all of the information is there, simply because it is not available. So you can see that Array 0 has some customData whereas Array 1 does not. Other elements may not even have a quoteData section etc.

So my blade template now has this information. I basically want to show all the data, but if it is not available, to just show an empty cell. So I have the following headers

<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> 

However, within the tbody, what I am doing seems very messy, the only way I can seem to get data in the correct place is if I check everything e.g.

@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

Is there a neater way to do this? Or is this my only option?

Thanks

5
  • You could do {{ @$array['leadData']['Client'] }} to suppress errors when the data's missing. Or pre-process the array in a way that fills any missing data with nulls. Commented Apr 25, 2016 at 14:47
  • @ceejayoz, interesting. Is it documented anywhere? If not, how exactly does it work? Commented Apr 25, 2016 at 14:48
  • 2
    @AlexeyMezenin @ is PHP's error suppression operator. See php.net/manual/en/language.operators.errorcontrol.php for details. It should be used rarely, but yours looks like a potentially appropriate use case. Commented Apr 25, 2016 at 14:51
  • @ceejayoz, thanks. Commented Apr 25, 2016 at 14:53
  • @AlexeyMezenin See @JoelHinz's answer for a better option. I forgot about Laravel's or. Commented Apr 25, 2016 at 14:53

2 Answers 2

4

Blade has a feature where you can use or to mean "echo this if it exists, or this if it doesn't". So you can do

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

And that basically results in what you want. Much cleaner, no? :)

Documentation: https://laravel.com/docs/5.2/blade#displaying-data - a few paragraphs down.

Sign up to request clarification or add additional context in comments.

5 Comments

I'd forgotten about this one. Nice.
Thats actually very interesting - it seems to also check if its empty which means I do not need to do this
@Joel Hinz, it's interesting too, definitely upvote. In documentation they say it's checking for isset(). But does it check for is_null() and empty?
I'll answer myself. Looked under the hood, it does only isset() check.
@AlexeyMezenin Nope. But isset() returns false for null values. Empty arrays I believe would be printed as 1, since the isset evaluates to true.
1

You also could do this more elegantly and shorter by iterating array with column names (used or '' as Joel advised):

{{ $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

6 Comments

Thanks, I gave this a try and it said that html eminities expects parameter 1 to be a string, array given
And in what line? Also, try to use {!! !!} instead of {{ }}
It is difficult to say because it says ErrorException in helpers.php line 469:
Ok, then just change {{ to {!! and it will not call htmlentities()
I think it was the column line it was complaining about. If I use {{ it complains about expecting parameter 1 to be a string. If I change it to {!! it gives me the error Array to string conversion
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.