Laravel 5.2 Blade - 未定义的偏移量:5 在我的循环中
Laravel 5.2 Blade - Undefined offset: 5 in my loop for
我正在为我的文章创建管理员,在我的资源编辑 post 我有一些问题,我想显示我正在编辑的 post 的所有标签,我正在像这样尝试:
我创建了一个变量数组,其中所有标签 id 与 post 编辑的关系:
$post_tags[0]->tag_id // it return some id of tag's post
现在我会显示所有标记的 post 就像在我的表单中选中的输入 edit.blade.php
@for ($i = 0; $i < count($all_tags); $i++)
@if($all_tags[$i]->id != $post_tags[$i]->tag_id)
<input style="cursor: pointer;" type="checkbox" name="tags[]" id=" {{$all_tags[$i]->slug}}" value="{{$all_tags[$i]->id}}" data-parsley-mincheck="2" class="flat"/>
@else
<input style="cursor: pointer;" type="checkbox" name="tags[]" id="{{$all_tags[$i]->slug}}" value="{{$all_tags[$i]->id}}" data-parsley-mincheck="2" class="flat" checked/>
@endif
@endfor
But i'm getting this error:
ErrorException in Collection.php line 1187: Undefined offset: 5 (View:
C:\xampp\htdocs\sofis\resources\views\admin\post\edit.blade.php)
这意味着 $post_tags[$i]
没有索引为 5 的元素。如果您不确定具有此索引的元素是否存在,则不应使用 $array[$index]
方法。或者你应该在使用前检查它:
@if (isset($post_tags[$i]) && $all_tags[$i]->id != $post_tags[$i]->tag_id)
我猜 $post_tags
没有索引 5,所以 $post_tags[$i]
失败了
Larvael 5.3 及以上
正如其他人所说,您需要检查以确保索引存在。您还可以通过使用 @foreach
而不是 @for
:
来简化您的逻辑
@foreach ($all_tags as $tag)
<input style="cursor: pointer;" type="checkbox" name="tags[]" id=" {{$tag->slug}}" value="{{$tag->id}}" data-parsley-mincheck="2" class="flat" {{ (isset($post_tags[$loop->index]) && $post_tags[$loop->index]->tag_id == $tag->id) ? 'checked' : '' }} />
@endforeach
您可以改进 Samsquanch 的答案以匹配低于 5.3 的版本。您可以通过在 foreach()
中定义 $key
来访问数组索引,如下所示。
@foreach ($all_tags as $key => $tag)
<input style="cursor: pointer;" type="checkbox" name="tags[]" id=" {{$tag->slug}}" value="{{$tag->id}}" data-parsley-mincheck="2" class="flat" {{ (isset($post_tags[$key]) && $post_tags[$key]->tag_id == $tag->id) ? 'checked' : '' }} />
@endforeach
我正在为我的文章创建管理员,在我的资源编辑 post 我有一些问题,我想显示我正在编辑的 post 的所有标签,我正在像这样尝试:
我创建了一个变量数组,其中所有标签 id 与 post 编辑的关系:
$post_tags[0]->tag_id // it return some id of tag's post
现在我会显示所有标记的 post 就像在我的表单中选中的输入 edit.blade.php
@for ($i = 0; $i < count($all_tags); $i++)
@if($all_tags[$i]->id != $post_tags[$i]->tag_id)
<input style="cursor: pointer;" type="checkbox" name="tags[]" id=" {{$all_tags[$i]->slug}}" value="{{$all_tags[$i]->id}}" data-parsley-mincheck="2" class="flat"/>
@else
<input style="cursor: pointer;" type="checkbox" name="tags[]" id="{{$all_tags[$i]->slug}}" value="{{$all_tags[$i]->id}}" data-parsley-mincheck="2" class="flat" checked/>
@endif
@endfor
But i'm getting this error:
ErrorException in Collection.php line 1187: Undefined offset: 5 (View: C:\xampp\htdocs\sofis\resources\views\admin\post\edit.blade.php)
这意味着 $post_tags[$i]
没有索引为 5 的元素。如果您不确定具有此索引的元素是否存在,则不应使用 $array[$index]
方法。或者你应该在使用前检查它:
@if (isset($post_tags[$i]) && $all_tags[$i]->id != $post_tags[$i]->tag_id)
我猜 $post_tags
没有索引 5,所以 $post_tags[$i]
失败了
Larvael 5.3 及以上
正如其他人所说,您需要检查以确保索引存在。您还可以通过使用 @foreach
而不是 @for
:
@foreach ($all_tags as $tag)
<input style="cursor: pointer;" type="checkbox" name="tags[]" id=" {{$tag->slug}}" value="{{$tag->id}}" data-parsley-mincheck="2" class="flat" {{ (isset($post_tags[$loop->index]) && $post_tags[$loop->index]->tag_id == $tag->id) ? 'checked' : '' }} />
@endforeach
您可以改进 Samsquanch 的答案以匹配低于 5.3 的版本。您可以通过在 foreach()
中定义 $key
来访问数组索引,如下所示。
@foreach ($all_tags as $key => $tag)
<input style="cursor: pointer;" type="checkbox" name="tags[]" id=" {{$tag->slug}}" value="{{$tag->id}}" data-parsley-mincheck="2" class="flat" {{ (isset($post_tags[$key]) && $post_tags[$key]->tag_id == $tag->id) ? 'checked' : '' }} />
@endforeach