douan3182 2018-07-01 15:51
浏览 60
已采纳

Laravel集合 - 仅当集合包含值时才回显值

I have a Laravel collection which returns different values. I want to fill the form always with the first set of the collection, so far there are data available.

Right now I am debugging and found an error. If there aren't data available I get Undefined offset: 0. This is how I check if data are available and if yes it should fill out the form.

value="@php if($data){ echo $data[0]->client_id; } @endphp"

This is my full code:

<form class="tab-pane active" id="tab_1_1">
    <div class="form-horizontal show" role="form" id="customer_details">
        <div class="row">
            <div class="col-md-6">
                <h5>Kundendetails</h5>
                <div class="form-group">
                   <label class="control-label col-sm-2" for="modal_cid">Kundennummer</label>
                   <div class="col-sm-10">
                       <input type="text" class="form-control" id="modal_cid" value="@php if($data){ echo $data[0]->client_id; } @endphp" disabled="">
                   </div>
                </div>
                <div class="form-group">
                   <label class="control-label col-sm-2" for="modal_title">Anrede</label>
                   <div class="col-sm-10">
                       <input type="text" class="form-control" id="modal_title" value="@php if($data){ echo $data[0]->title; } @endphp" autocomplete="honorific-prefix">
                   </div>
                </div>
                <div class="form-group">
                   <label class="control-label col-sm-2" for="modal_academic_title">Titel</label>
                   <div class="col-sm-10">
                       <input type="text" class="form-control" id="modal_academic_title" value="@php if($data){ echo $data[0]->academic_title; } @endphp" autocomplete="honorific-prefix">
                   </div>
                </div>
                <div class="form-group">
                   <label class="control-label col-sm-2" for="modal_fname">Vorname</label>
                   <div class="col-sm-10">
                       <input type="name" class="form-control" id="modal_fname" value="@php if($data){ echo $data[0]->first_name; } @endphp" autocomplete="given-name">
                   </div>
                </div>
            </div>
       </div>
    </div>
</form>

How can I fix this error and only fill out the form with the first set/row of the collection?

I have already tried out those solutions. Not working..

  1. value="@php if($data){ echo $data[0]->account_account_holder; } @endphp"

  2. value="@php if($data[0){ echo $data[0]->account_account_holder; } @endphp"

  3. value="@php if($data[0]->account_account_holder){ echo $data[0]->account_account_holder; } @endphp"

  • 写回答

1条回答 默认 最新

  • doucaigai7176 2018-07-01 15:59
    关注

    Use isset() to check it first

    @php if(isset($data[0])){ echo $data[0]->client_id; } @endphp
    

    for more sure

    @php if(isset($data[0]->client_id)){ echo $data[0]->client_id; } @endphp
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?