php - foreach() argument must be of type array|object, null given in laravel 8
Get the solution ↓↓↓I am trying to insert data by Select users from checkbox's and input different point for each user but when I submit I got this error
and is that the correct way to insert multi value coming from checkboxes in the model with different point for each user
Can someone please help me to find out Where I am wrong?
<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>
the ActionUserPush function
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);}
Answer
Solution:
You don't have any input with nameuser_id
and in your form you can not define the input name dynamically like you have done. You should have to use a fixed name for it. Change your form input to:
<input type="checkbox" name="user_id[]" value="{{$user->id}}" class="custom-control-input" >
Then you can access it by using$request->user_id
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: warning: a non-numeric value encountered in
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.