gpt4 book ai didi

php - undefined offset :1- Laravel Error exception

转载 作者:行者123 更新时间:2023-11-29 21:04:43 24 4
gpt4 key购买 nike

我知道有几个链接提到了此错误,但我仍然无法找到问题的确切解决方案。我有两个名为 admin_menu_id[] 和 admin_sub_menu_id[] 的复选框,我想将其值插入到数据库中。到目前为止我已经做到了:-

查看页面

 @if(is_array($roledatas) && count($roledatas)>0)
<?php $count = 1;?>
@foreach($roledatas as $roleKey=>$roleVal)
<ul id="package_{{ $count }}" class="accordion-toggle parentclasstr" data-toggle="collapse" data-parent="#OrderPackages" data-target=".packageDetails{{ $count }}">

<li><input type="checkbox" id="menuChk" value= "<?php echo $id = $roleVal['id']; ?>" name="admin_menu_id[]" /><strong>{{ $roleVal['name'] }}</strong></li>
<li><i class="indicator glyphicon glyphicon-chevron-up pull-right"></i></li>

@if(isset($roleVal['child']) && is_array($roleVal['child']) && count($roleVal['child'])>0)
@foreach($roleVal['child'] as $childKey=>$childVal)
<ul class="hiddenRow collapse packageDetails{{ $count }}" bgcolor="#C1C1C1">

<li><input type="checkbox" id="submenuChk" value="<?php echo $id = $childVal['id']; ?>" name="admin_sub_menu_id[]" />{{ $childVal['name'] }}</li>
<li></li>
</ul>
@endforeach
@endif
</ul>
<?php $count++; ?>
@endforeach
@endif

Controller :-

$data = $request->all();
$user_type_id = $data['user_type_id'];
$admin_menu_id = $data['admin_menu_id'];
$admin_sub_menu_id = $data['admin_sub_menu_id'];

for ($i = 0; $i < count($data['admin_sub_menu_id']); $i++)
{
$role = new RoleMenu;
$role->user_type_id = $user_type_id;
$role->admin_menu_id = $admin_menu_id[$i];
$role->admin_sub_menu_id = $admin_sub_menu_id[$i];
$role->save();
}

当我检查一个 admin_menu_id 复选框和一个 admin_sub_menu_id 复选框时,它会插入数据,但当尝试检查多个复选框时,它会抛出未定义偏移的错误。我对 laravel 和这些数组游戏完全陌生!请帮我解决这个问题。

最佳答案

Controller 中的循环从 0 到 count($data['admin_sub_menu_id']),您确定提交的 $admin_menu_id 的数量对应于 count($data['admin_sub_menu_id']) 吗?如果没有,您将始终收到该错误。

我建议你有2个循环,外循环控制admin_menu_id,内循环控制admin_sub_menu_id!!

关于php - undefined offset :1- Laravel Error exception,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36913979/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com