foreach()参数必须是数组类型|对象,laravel 8中给定的空值

ruoxqz4g  于 2022-12-01  发布在  其他
关注(0)|答案(3)|浏览(147)

我尝试通过从复选框中选择用户并为每个用户输入不同的点来插入数据,但提交时出现此错误

这是插入多个值的正确方法,这些值来自模型中的复选框,每个用户都有不同的点
有人能帮我找出我错在哪里吗?

<form method="post" action="{{route('user.action.push')}}" >
@csrf
                <table  class="display table table-bordered table-separated" >
                        <thead>
                         <th scope="row">
                <label class="custom-control custom-checkbox">
                    <input id="selectAll"  class="custom-control-input" type="checkbox">
                    <span class="custom-control-label"></span>
                    <span style="background-color:red;" class="custom-control-description sr-only"></span>
                </label>
            </th>

                                <th>Name</th>
                                <th>Email</th>
                                <th>point</th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach($users as $user)
                            <tr>
  <td scope="row">
                <label class="custom-control custom-checkbox">
                    <input type="checkbox" name="user_[{{$user->id}}]" value="{{$user->id}}" class="custom-control-input" >
                    <span class="custom-control-label"></span>
                    <span style="background-color:red;" class="custom-control-description sr-only"></span>
                </label>
            </td>

    <td style="color:white">{{$user->name}}</td>

    <td style="color:white">{{$user->email}}</td>

    <td style="color:white"><input type="text" name="point_[{{$user->id}}]" >
</td>
                            </tr>                   
        @endforeach 
                        </tbody>
                        <tfoot>
                            <tr>
                                <th>Select</th>
                                <th>Name</th>
                                <th>Email</th>
                                <th>point</th>
                            </tr>
                        </tfoot>
                    </table>
                    </div>
                </div>
                <!-- /.box-body -->
              </div>
              <!-- /.box -->      
            </div>  
<div class="col-3">
             <div class="box">
                <div class="box-header with-border">
                  <h3 class="box-title">Select Action</h3>
                </div>
                <!-- /.box-header -->
                <div class="box-body">

 <div class="form-group">
        <h5>Select Action <span class="text-danger">*</span></h5>
        <div class="controls">
            <select  name="action_id"  class="form-control" >

                <option disabled="" selected="">Select Action</option>
@foreach($actions as $action)
                <option value="{{$action->id}}">{{$action->action_title}}</option>
@endforeach
            </select>
            <br>
                <button type="submit" class="btn btn-rounded btn-primary"> send</button>
         </form>

操作用户推送函数

public function ActionUserPush(Request $request){
            
       foreach($request->user_id as $user_id){
                $point = $request->point_[$user_id];
    
               ActionUser::insert([
                  
                    'user_id'=>$user_id,
                    'point' =>$point,
                    'action_id'=>$request->action_id,
    
                ]); }
    
            $notification = array(
                'message' => ' Action Activited Successfully',
                'alert-type' => 'success'
            );
            return redirect()->route('user_action_view')->with($notification);}
pcww981p

pcww981p1#

您没有任何名称为user_id的输入,而且您无法在表单中动态定义输入名称。您必须使用固定名称。请将表单输入变更为:

<input type="checkbox" name="user_id[]" value="{{$user->id}}" class="custom-control-input" >

然后可以使用$request->user_id访问它

yb3bgrhw

yb3bgrhw2#

你必须使用如下

name="user_id[]"
yrdbyhpb

yrdbyhpb3#

您没有任何具有nameuser_id的输入,而且在表单中您无法像以前那样动态定义输入名称。您应该使用固定名称。请将表单输入更改为:
id}}”class=“custom-control-input”〉然后,您可以使用$request-〉user_id访问它

相关问题