首页 > 解决方案 > 有没有更好的方法来检查一个集合是否包含一个字段然后是多个 isset?

问题描述

所以我在我的网站上有一个页面,它为每个企业创建一张卡片,其中包含与企业相关的多个信息。并非所有内容都包含数据,这意味着某些关系不包含数据,我想知道是否有更好的方法来做到这一点,然后简单地使用多个 if isset 语句?我在下面的代码之外有一个 foreach 循环来循环每个业务,有些将包含所有数据,而有些则不包含。这对于下面来说还不错,我遇到的问题是,我有几页包含大量数据,那么这将是很多技巧

<div class="card-spacer bg-white card-rounded flex-grow-1">
<!--begin::Row-->
<div class="row m-0">
    <div class="col px-8 py-6 mr-8">
        <div class="font-size-sm text-muted font-weight-bold">Channel
        </div>
        <div class="font-size-h4 font-weight-bolder">
            @if(isset($business->unleashedcustomer()->first()->channel->name))
                {{$business->unleashedcustomer()->first()->channel->name}}
            @endif
        </div>
    </div>
    <div class="col px-8 py-6">
        <div class="font-size-sm text-muted font-weight-bold">Classification
        </div>
        <div class="font-size-h4 font-weight-bolder">
            @if(isset($business->unleashedcustomer()->first()->secondary_classification->name))
                {{$business->unleashedcustomer()->first()->secondary_classification->name}}
            @endif
        </div>
    </div>
</div>
<!--end::Row-->
<!--begin::Row-->
<div class="row m-0">
    <div class="col px-8 py-6 mr-8">
        <div class="font-size-sm text-muted font-weight-bold">Tier</div>
        <div class="font-size-h4 font-weight-bolder">
            {{$business->unleashedcustomer()->first()->tier}}</div>
    </div>
    <div class="col px-8 py-6">
        <div class="font-size-sm text-muted font-weight-bold">Growth Rate</div>
        <div class="font-size-h4 font-weight-bolder">
            {{$business->unleashedcustomer()->first()->growth_rate}}</div>
    </div>
</div>
<!--end::Row-->
<!--begin::Row-->
<div class="row m-0">
    <div class="col px-8 py-6 mr-8">
        <div class="font-size-sm text-muted font-weight-bold">Last Purchase</div>
        <div class="font-size-h4 font-weight-bolder">
            {{$business->unleashedcustomer()->first()->date_last_purchase}}</div>
    </div>
    <div class="col px-8 py-6">
        <div class="font-size-sm text-muted font-weight-bold">Last Interaction</div>
        <div class="font-size-h4 font-weight-bolder">
            {{$business->unleashedcustomer()->first()->vms_last_interaction_date}}
        </div>
    </div>
</div>
<!--end::Row-->

标签: phpeloquentlaravel-8isset

解决方案


推荐阅读