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 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python