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 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法