嘿,所以我正在尝试更改数据并更新它们。但是当我按下update按钮时出现这个错误:“调用数组上的成员函数save()”,所以这是我的代码:
控制器:
public function update(Request $r)
{
$update_id = $r->uid;
$name = $r->uname;
$email = $r->umail;
$add = $r->uadd;
$update = App\Models\Emp_model::find($update_id);
$update['name']=$name;
$update['email']=$email;
$update['address']=$add;
$updated=$update->save();
if ($updated) {
return redirect('show')->with('message', 'Data added successfully!');
}
}路由:
Route::get('/show', [Employee::class, 'fetch']);
Route::post('/update_data', [Employee::class, 'update']);操作:
<form action="{{ url('/update_data') }}" method="post">发布于 2020-12-26 08:40:12
我建议您简单地使用update方法:
$el = App\Models\Emp_model::find($update_id);
$el->update([
'name' => $name ,
'email' => $email ,
'address' => $add ,
]);发布于 2020-12-26 08:51:10
$r->uid似乎与模型的主键列不对应。所以你可以试试下面的
public function update(Request $r)
{
<!-- Replace uid with whatever is the column name -->
$employee = App\Models\Emp_model::where('uid', $r->uid)->firstOrFail();
$employee->update([
'name' => $r->uname,
'email' => $r->uemail,
'add' => $r->uadd,
]);
return redirect('show')->with('message', 'Data added successfully!');
}您可以使用id作为路径参数来更新命名路径
Route::post('/update_data/{id}', [Employee::class, 'update'])->name('emp.update');然后在刀片视图中进行编辑
<!-- Replace $emp with whatever is the variable name -->
<form action="{{ route('emp.update', ['id' => $emp->id]) }}" method="post">发布于 2020-12-26 09:18:22
控制器:
function update(Request $r)
{
$update = Emp_model::find($r->uid);
$update->uname=$r->uname;
$update->umail=$r->umail;
$update->uadd=$r->uadd;
$updated=$update->save();
if ($updated) {
return redirect('show')->with('message', 'Data added successfully!');
}
}https://stackoverflow.com/questions/65452846
复制相似问题