I want to update all the rows having role_id of "1" in laravel. But this returning the following error :
Illuminate\Database\QueryException
SQLSTATE[22007]: Invalid datetime format: 1366 Incorrect integer value: '["2","3"]' for column 'menu_id' at row 1 (SQL: update role_menus
set menu_id
= ["2","3"], role_id
= 1, write
= ["1","0"], read
= ["1","1"], edit
= ["0","1"], delete
= ["0","1"], created_at
= 2022-03-05 04:22:59, role_menus
.updated_at
= 2022-03-05 04:22:59 where role_id
= 1)
http://127.0.0.1:8000/api/update_role/1
My codes so far are :
Controller :
public function updateRole(Request $request, $id){
$update_role=array(
'menu_id'=> $request->menu_id,
'role_id'=> $request->role_id,
'write'=> $request->write,
'read'=> $request->read,
'edit'=> $request->edit,
'delete'=> $request->delete,
'created_at' => Carbon::now()
);
$role_updated = RoleMenu::where('role_id','=',$id)->update($update_role);
if($role_updated){
echo $message = "Role Updated Successfully";
}else{
echo $message = "Something Went Wrong !";
}
// return json_encode($message);
}
Codes from my html :
<form action="http://127.0.0.1:8000/api/update_role/1" method="POST">
<div>
<input type="text" name="menu_id[]" value="2">
<input type="text" name="role_id[]" value="1">
<input type="text" name="write[]">
<input type="text" name="read[]">
<input type="text" name="edit[]">
<input type="text" name="delete[]">
</div>
<div>
<input type="text" name="menu_id[]" value="3">
<input type="text" name="role_id[]" value="1">
<input type="text" name="write[]">
<input type="text" name="read[]">
<input type="text" name="edit[]">
<input type="text" name="delete[]">
</div>
<button type="submit">Submit</button>
</form>
CodePudding user response:
Change your all form input fields as below
<input type="text" name="role[0]['menu_id']" value="2">Menu
..........
Change in your controller for prepareing array and storing in table as below
foreach($request->role as $role) {
$update_role = array(
'menu_id'=> $request->menu_id,
'role_id'=> $request->role_id,
'write'=> $request->write,
'read'=> $request->read,
'edit'=> $request->edit,
'delete'=> $request->delete,
'created_at' => Carbon::now()
);
$role_updated = RoleMenu::where('role_id','=',$id)->update($update_role);
}
Hope this will work
CodePudding user response:
It should look like this:
HTML
<input type="text" name="menu_id" value="2">
<input type="text" name="role_id" value="1">
<input type="text" name="write">
<input type="text" name="read">
<input type="text" name="edit">
<input type="text" name="delete">
And PHP
$update_role=array(
'menu_id'=> $request->menu_id,
'role_id'=> $request->role_id,
'write'=> $request->write,
'read'=> $request->read,
'edit'=> $request->edit,
'delete'=> $request->delete,
'created_at' => Carbon::now()
);
$role_updated = RoleMenu::where('role_id','=',$id)->update($update_role);