Laravel 7 - 更新用户
Laravel 7 - Update user
我的用户 'edit' 操作有问题。我可以创建新用户,但无法更新它们。
这是我在UserController
中的编辑方法:
/**
* Show the form for editing the specified resource.
*
* @param \App\User $user
* @return \Illuminate\Http\Response
*/
public function edit($userID)
{
//
$user = User::query()->findOrFail($userID);
$roles = Role::pluck('nombre_rol','id');
$departments = Department::all(['id','department_name']);
return view('user.edit',compact('user','roles','departments'));
}
这是UserController
中的更新方法:
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\User $user
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $usuarioID)
{
$user = User::query()->findOrFail($usuarioID);
$user->update($request->only('name','email','password', 'roles', 'departament_id'));
$user->roles()->sync($request->roles);
return back()->with('Success','Usuario actualizado con éxito');
}
这是编辑视图:
<body id="bodyForm">
<div class="container">
<h2 class="text-center txt-unam-gold">@lang('edit.Editar') @lang('edit.Usuario')</i></h1>
<h4 class="text-center text-secondary">{{$user->name}}</h1>
<br>
<form method="POST" action="{{route('Usuario.update', $user->id)}} class="user" enctype="multipart/form-data"">
{!! method_field('PUT') !!}
@include('user.form')
<!--Roles-->
<br>
<div class="row justify-content-center text-center">
<div class="col-md-6">
<h5 class="txt-unam text-center text-primary">@lang('edit.Roles')</h5>
@foreach($roles as $id => $name)
<input type="checkbox" value="{{$id}}" {{$user->roles->pluck('id')->contains($id) ? 'checked' : ''}} name="roles[]">
{{ $name }}
@endforeach
</div>
</div>
<br>
<div class="text-center">
<button class="btn btn-warning" type="submit" value="Acttualizar">@lang('edit.Actualizar')</button>
<button class="btn btn-danger" onclick="cancel()" value="Cancelar">@lang('edit.Cancelar')</button>
</div>
</form>
</div>
</body>
这是我在编辑视图中 @include
的表格。
<div class="row justify-content-center py-3">
<div class="col-md-5 mr-2">
<label class="txt-unam text-primary" for="name">@lang('form.Nombre')</label>
<input value="{{isset($user->name)?$user->name:old('name')}}" required type="text" name="name" id="name" class="form-control text-muted">
</div>
<div class="col-md-5">
<label class="txt-unam text-primary" for="email">@lang('form.Correo')</label>
<input value="{{isset($user->email)?$user->email:old('email')}}" required type="text" name="email" id="email" class="form-control text-muted">
</div>
</div>
<div class="row justify-content-center py-1">
<div class="col-md-5 mr-2">
<label class="txt-unam text-primary">@lang('form.Contraseña')</label>
<input value="{{--decrypt($usuario->password) ?? old('password')--}}" class="form-control" type="password" name="password" id="password">
</div>
<div class="col-md-5">
<label class="txt-unam text-primary">@lang('form.Seleccionar')</label>
<select name="department_id" id="department_id" class="form-control">
<option selected disabled value="">@lang('form.Seleccionar')</option>
@foreach ($departments as $department)
<option value="{{$department->id}}">{{$department->department_name}}</option>
@endforeach
</select>
</div>
</div>
当我按下编辑按钮时出现错误:
419 Page Expired
用户图片table结构
如果您在提交编辑表单后遇到此错误,请将 @csrf
放在 {!! method_field('PUT') !!}
之后
应该喜欢,
<form method="POST" action="{{route('Usuario.update', $user->id)}} class="user" enctype="multipart/form-data"">
{!! method_field('PUT') !!}
@csrf // For lower version use - {{ csrf_field() }}
@include('user.form')
<!--Roles-->
<br>
<div class="row justify-content-center text-center">
<div class="col-md-6">
<h5 class="txt-unam text-center text-primary">@lang('edit.Roles')</h5>
@foreach($roles as $id => $name)
<input type="checkbox" value="{{$id}}" {{$user->roles->pluck('id')->contains($id) ? 'checked' : ''}} name="roles[]">
{{ $name }}
@endforeach
</div>
</div>
<br>
<div class="text-center">
<button class="btn btn-warning" type="submit" value="Acttualizar">@lang('edit.Actualizar')</button>
<button class="btn btn-danger" onclick="cancel()" value="Cancelar">@lang('edit.Cancelar')</button>
</div>
</form>
HTML 表单只支持 POST 和 GET,所以如果你想执行更新操作,你需要在开始和结束表单标签之间添加 @method('PUT')
然后添加 @csrf
仍在您的表单标签中
<form method="POST" action="{{route('user.update', $user->id)}} class="user" enctype="multipart/form-data"">
@method('PUT')
@csrf
</form>
我的用户 'edit' 操作有问题。我可以创建新用户,但无法更新它们。
这是我在UserController
中的编辑方法:
/**
* Show the form for editing the specified resource.
*
* @param \App\User $user
* @return \Illuminate\Http\Response
*/
public function edit($userID)
{
//
$user = User::query()->findOrFail($userID);
$roles = Role::pluck('nombre_rol','id');
$departments = Department::all(['id','department_name']);
return view('user.edit',compact('user','roles','departments'));
}
这是UserController
中的更新方法:
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\User $user
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $usuarioID)
{
$user = User::query()->findOrFail($usuarioID);
$user->update($request->only('name','email','password', 'roles', 'departament_id'));
$user->roles()->sync($request->roles);
return back()->with('Success','Usuario actualizado con éxito');
}
这是编辑视图:
<body id="bodyForm">
<div class="container">
<h2 class="text-center txt-unam-gold">@lang('edit.Editar') @lang('edit.Usuario')</i></h1>
<h4 class="text-center text-secondary">{{$user->name}}</h1>
<br>
<form method="POST" action="{{route('Usuario.update', $user->id)}} class="user" enctype="multipart/form-data"">
{!! method_field('PUT') !!}
@include('user.form')
<!--Roles-->
<br>
<div class="row justify-content-center text-center">
<div class="col-md-6">
<h5 class="txt-unam text-center text-primary">@lang('edit.Roles')</h5>
@foreach($roles as $id => $name)
<input type="checkbox" value="{{$id}}" {{$user->roles->pluck('id')->contains($id) ? 'checked' : ''}} name="roles[]">
{{ $name }}
@endforeach
</div>
</div>
<br>
<div class="text-center">
<button class="btn btn-warning" type="submit" value="Acttualizar">@lang('edit.Actualizar')</button>
<button class="btn btn-danger" onclick="cancel()" value="Cancelar">@lang('edit.Cancelar')</button>
</div>
</form>
</div>
</body>
这是我在编辑视图中 @include
的表格。
<div class="row justify-content-center py-3">
<div class="col-md-5 mr-2">
<label class="txt-unam text-primary" for="name">@lang('form.Nombre')</label>
<input value="{{isset($user->name)?$user->name:old('name')}}" required type="text" name="name" id="name" class="form-control text-muted">
</div>
<div class="col-md-5">
<label class="txt-unam text-primary" for="email">@lang('form.Correo')</label>
<input value="{{isset($user->email)?$user->email:old('email')}}" required type="text" name="email" id="email" class="form-control text-muted">
</div>
</div>
<div class="row justify-content-center py-1">
<div class="col-md-5 mr-2">
<label class="txt-unam text-primary">@lang('form.Contraseña')</label>
<input value="{{--decrypt($usuario->password) ?? old('password')--}}" class="form-control" type="password" name="password" id="password">
</div>
<div class="col-md-5">
<label class="txt-unam text-primary">@lang('form.Seleccionar')</label>
<select name="department_id" id="department_id" class="form-control">
<option selected disabled value="">@lang('form.Seleccionar')</option>
@foreach ($departments as $department)
<option value="{{$department->id}}">{{$department->department_name}}</option>
@endforeach
</select>
</div>
</div>
当我按下编辑按钮时出现错误:
419 Page Expired
用户图片table结构
如果您在提交编辑表单后遇到此错误,请将 @csrf
放在 {!! method_field('PUT') !!}
应该喜欢,
<form method="POST" action="{{route('Usuario.update', $user->id)}} class="user" enctype="multipart/form-data"">
{!! method_field('PUT') !!}
@csrf // For lower version use - {{ csrf_field() }}
@include('user.form')
<!--Roles-->
<br>
<div class="row justify-content-center text-center">
<div class="col-md-6">
<h5 class="txt-unam text-center text-primary">@lang('edit.Roles')</h5>
@foreach($roles as $id => $name)
<input type="checkbox" value="{{$id}}" {{$user->roles->pluck('id')->contains($id) ? 'checked' : ''}} name="roles[]">
{{ $name }}
@endforeach
</div>
</div>
<br>
<div class="text-center">
<button class="btn btn-warning" type="submit" value="Acttualizar">@lang('edit.Actualizar')</button>
<button class="btn btn-danger" onclick="cancel()" value="Cancelar">@lang('edit.Cancelar')</button>
</div>
</form>
HTML 表单只支持 POST 和 GET,所以如果你想执行更新操作,你需要在开始和结束表单标签之间添加 @method('PUT')
然后添加 @csrf
仍在您的表单标签中
<form method="POST" action="{{route('user.update', $user->id)}} class="user" enctype="multipart/form-data"">
@method('PUT')
@csrf
</form>