我正在为学校做一个项目,我遇到了一个小问题。我想替换用户表中的数字("标记"列),并且我在调用更新时遇到问题。
PS:我可以毫无问题地添加,编辑和删除。
提前致谢!干杯!
我的更新方法:
public function update(Request $request, $id, $disable = false)
{
$user = User::findOrfail($id);
if($disable){
$user->flags = 1;
$user->save();
return redirect()->route('admin.index');
} else {
$rules = [
'name' => 'required|max:255',
'email' => 'required|email|max:255',
'password' => 'required|confirmed|min:6',
'institution_id' => 'required',
];
$validator = Validator::make($request->all(), $rules);
if ($validator->fails()) {
return redirect()->route('admin.edit', [$id])->withErrors($validator)->withInput();
}
$user->name = $request->get('name');
$user->email = $request->get('email');
$alt_email = $request->get('alt_email');
if ($alt_email) {
$user->alt_email = $alt_email;
}
$photo_url = $request->get('photo_url');
if ($photo_url) {
$user->photo_url = $photo_url;
}
$profile_url = $request->get('profile_url');
if ($profile_url) {
$user->profile_url = $profile_url;
}
$user->institution_id = $request->get('institution_id');
$user->password = bcrypt($request->get('password'));
$user->position = $request->get('position');
$user->role = $request->get('role');
$message = ['message_success' => 'User ' . $user->name . ' update successfully!'];
if (!$user->save()) {
$message = ['message_error' => 'Failed to create user!'];
}
}
return redirect()->route('admin.index')->with($message);
}
我想要进行更改的按钮:
<td>
<a class="btn btn-primary" style="color: #ffffff" href="{{route('admin.update',[$user->id, true]) }}"
>Disable</a>
</td>
编辑:
我的路线:
Route::resource('admin', 'AdminController');