laravel - 在laravel中更新表单后无法刷新页面
问题描述
您好,我需要使用 Laravel 在模型内部进行更新,
问题是当我单击更新按钮(他在代码中的名称是 Modifier)时,页面没有刷新。我尝试使用a标签代替button。
并使用 a 标签刷新页面,但我的数据没有更新。
有人可以告诉我代码中的问题在哪里
索引视图代码:
<table class="table table-bordered table-left">
<thead>
<tr>
<th>#</th>
<th>Nom</th>
<th>Email</th>
<th>Rôle</th>
<th>Action</th>
</tr>
</thead>
<tbody>
@foreach ($users as $key=>$user)
<tr>
<td>{{ $key+1 }}</td>
<td>{{ $user->name }}</td>
<td>{{ $user->email }}</td>
<td>
@if ($user->is_admin==1)Administrateur
@else Caissier
@endif
</td>
<td>
<div class="btn-group">
<a class="btn btn-success" href="" data-toggle="modal" data-target="#edituser{{ $user->id }}">
<i class="fas fa-edit"></i>
</a>
<a href="" class="btn btn-danger">
<i class="fas fa-trash"></i>
</a>
</div>
</td>
</tr>
{{-- edit model --}}
<div class="modal right fade" id="edituser{{ $user->id }}" data-backdrop="static" data-keyboard="false" tabindex="-1" aria-labelledby="staticBackdropLabel" aria-hidden="true">
<div class="modal-dialog modal-sm">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title" id="staticBackdropLabel">Modifier l'utilisateur</h4>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<form action="{{ route('users.update', $user->id) }}" method="post">
@csrf
@method('put')
<div class="form-group">
<label for="name">Nom</label>
<input type="text" value="{{ $user->name }}" name="name" class="form-control">
</div>
<div class="form-group">
<label for="email">Email</label>
<input type="email" value="{{ $user->email }}" name="email" class="form-control">
</div>
<div class="form-group">
<label for="name">Mot de passe</label>
<input type="password" readonly name="password" value="{{ $user->password }}" class="form-control">
</div>
{{-- <div class="form-group">
<label for="name">Confirmer le mot de passe</label>
<input type="password" name="confirm_password" class="form-control">
</div> --}}
<div class="form-group">
<label for="name">Rôle</label>
<select name="is_admin" id="" class="form-control">
<option value="1" @if ($user->is_admin==1)
selected
@endif>Administrateur</option>
<option value="2" @if ($user->is_admin==2)
selected
@endif>Caissier</option>
</select>
</div>
<div >
<button class="btn btn-success btn-block">Modifier</button>
</div>
</form>
</div>
</div>
</div>
</div>
@endforeach
</tbody>
</table>
控制器代码:
public function update(Request $request, $id)
{
$users = User::find($id);
if (!$users) {
return back()->with('Error','user created');
}
$users->update($request->all());
return back()->with('Success','user created');
}
解决方案
您的 button
<button class="btn btn-success btn-block">Modifier</button>
应该是
<button class="btn btn-success btn-block" type="submit">Modifier</button>
为了将表单提交给您的控制器。
推荐阅读
- reactjs - React:更新 Firebase 适用于输入,不适用于选择
- reactjs - 如何在将对象作为道具传递时测试组件?
- node.js - 快速查询字符串检查并发送给客户端
- javascript - 如何检测使用 javascript 关闭整个浏览器?
- ios - 当您的数据源是 Results 对象时,如何在表格视图中移动行?
- jenkins - 从 ACR 拉取时,ImagePullBackOff 带有“rpc 错误:代码 = 未知 desc = 无法从 AKS 拉取和解压缩图像”
- flutter - 使用列表中的地图转换列表列表
- django - 如何列出组中的所有频道名称?
- node.js - 创建添加到数组字段的新子项
- c# - 重叠 DockStyle.Top 面板