laravellaravel-blade

Laravel collection does not contains


I have a Laravel Collection and I am trying to get it to show results which do not contain a value.

I am aware of $data->contains(key, value), it basically needs to be the opposite of this. I am trying to do it within a blade template, with code like so;

    @if(!$orders->contains('order_status', 'Complete'))

and

    @if($orders->contains('order_status', 'Complete') === false)

but neither works as expected.

Any ideas or alternative approaches (want to try and keep the logic in blade if possible)?

thanks


var_dump of an item from the collection

array(2) {
  [0]=>
  array(26) {
    ["id"]=>
    int(1)
    ["user_id"]=>
    int(3)
    ["cv"]=>
    int(0)
    ["cv_details"]=>
    NULL
    ["cl"]=>
    int(1)
    ["cl_details"]=>
    string(0) ""
    ["ja"]=>
    int(1)
    ["ja_details"]=>
    string(0) ""
    ["order_status"]=>
    string(13) "PreAuthorized"
    ["advisor_id"]=>
    NULL
    ["created_at"]=>
    string(19) "2017-07-18 10:38:06"
    ["updated_at"]=>
    string(19) "2017-07-18 10:38:22"
    ["preAuthId"]=>
    string(8) "29506753"
    ["days"]=>
    int(3)
    ["customer_value"]=>
    int(86)
    ["due"]=>
    string(19) "2017-07-21 10:38:22"
    ["ck_fee"]=>
    float(25.65)
    ["cv_company"]=>
    NULL
    ["cv_role"]=>
    NULL
    ["cl_company"]=>
    string(0) ""
    ["cl_role"]=>
    string(0) ""
    ["ja_company"]=>
    string(0) ""
    ["ja_role"]=>
    string(0) ""
    ["cv_sector"]=>
    string(2) "IT"
    ["cl_sector"]=>
    string(2) "IT"
    ["ja_sector"]=>
    string(2) "IT"
}

more code;

     @if($orders->contains('order_status', 'Complete'))
                    <h4>Completed orders</h4>
                    <table class="table table-hover">
                        <thead>
                        <tr>
                            <th>Order #</th>
                            <th>Cost</th>
                            <th>Completed On</th>
                            <th>View Files</th>
                        </tr>
                        </thead>
                        <tbody>
                        @foreach($orders as $order)
                            @if($order->order_status == 'Complete')

                                <tr>
                                    <td>{{ $order->id }}</td>
                                    <td>£ {{ $order->customer_value }}</td>
                                    <td>{{ $order->updated_at }}</td>
                                    <td><a href="/view-order/{{ $order->id }}">View Files</a></td>
                                </tr>
                            @endif
                        @endforeach

                        </tbody>
                    </table>
                    </tbody>
                    </table>
                @endif
                @if(!$orders->contains('order_status', 'Complete'))
                    <h4>Orders in progress</h4>
                    <table class="table table-hover">
                        <thead>
                        <tr>
                            <th>Order #</th>
                            <th>Placed on</th>
                            <th>Due by</th>
                            <th>Cost</th>
                            <th>Status</th>
                        </tr>
                        </thead>
                        <tbody>
                        @foreach($orders as $order)
                            @if($order->order_status !== 'Complete')
                                <tr>
                                    <td>{{ $order->id }}</td>
                                    <td>{{ $order->created_at }}</td>
                                    <td>{{ $order->due }}</td>
                                    <td>£ {{ $order->customer_value }}</td>
                                    <td>{{ $order->order_status }}</td>
                                </tr>
                            @endif
                        @endforeach

                        </tbody>
                    </table>
                @endif

Solution

  • Turns out this was just a logic error as discussed in chat.

    @if(!$orders->contains('order_status', 'Complete'))
    

    This is saying to only display the table when where are no orders with the order_status 'Complete' instead of the intended "show this if there are any orders that are not 'Complete'".