首页 > 解决方案 > 在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">&times;</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');
    }

标签: laravelbootstrap-4laravel-7

解决方案


您的 button <button class="btn btn-success btn-block">Modifier</button>应该是 <button class="btn btn-success btn-block" type="submit">Modifier</button>为了将表单提交给您的控制器。


推荐阅读