请求$ data2 is_null 或为空时, 总是显示错误
Undefined offset: 0
在线
{{$data2[$key]->month}}
我的刀片
@foreach($data1 as $key => $value)
<tr>
<td align="center" style="padding: 10px">{{$data1[$key]->id}}</td>
<td align="center" style="padding: 10px">{{$data1[$key]->Parameters->name}}</td>
<td align="center" align="center" style="padding: 10px">{{$data1[$key]->month}}</td>
@if(isset($data2))
<td align="center" align="center" style="padding: 10px">{{$data2[$key]->month}}</td>
@endif
</tr>
@endforeach
我怎么能解决这个问题?
答案 0 :(得分:1)
您正在检查$data3
的isset,但您需要检查$data2[$key]
@foreach($data1 as $key => $d1)
<tr>
<td align="center" style="padding: 10px">{{$data1[$key]->id}}</td>
<td align="center" style="padding: 10px">{{$data1[$key]->Parameters->name}}</td>
<td align="center" align="center" style="padding: 10px">{{$data1[$key]->month}}</td>
@if(isset($data2[$key]))
<td align="center" align="center" style="padding: 10px">{{$data2[$key]->month}}</td>
@endif
</tr>
@endforeach
您也可以将$data1[$key]
更改为$d1
@foreach($data1 as $key => $d1)
<tr>
<td align="center" style="padding: 10px">{{$d1->id}}</td>
<td align="center" style="padding: 10px">{{$d1->Parameters->name}}</td>
<td align="center" align="center" style="padding: 10px">{{$d1->month}}</td>
@if(isset($data2[$key]))
<td align="center" align="center" style="padding: 10px">{{$data2[$key]->month}}</td>
@endif
</tr>
@endforeach
答案 1 :(得分:0)
试试这个,检查$data2[$key]
是否设置@if(isset($data2[$key]))
:
@foreach($data1 as $key => $d1)
<tr>
<td align="center" style="padding: 10px">{{$data1[$key]->id}}</td>
<td align="center" style="padding: 10px">{{$data1[$key]->Parameters->name}}</td>
<td align="center" align="center" style="padding: 10px">{{$data1[$key]->month}}</td>
@if(isset($data2[$key]))
<td align="center" align="center" style="padding: 10px">{{$data2[$key]->month}}</td>
@endif
</tr>
@endforeach