代码之家  ›  专栏  ›  技术社区  ›  Kalana Mihiranga

无法使用laravel销毁函数删除表上的最后一个数据

  •  0
  • Kalana Mihiranga  · 技术社区  · 6 年前

    我创建了一个laravel destroy函数,并使用一个modal来删除数据。但我无法删除数据库中的所有数据。无法删除最后一个。我认为这是可以预见的。

    这是视图中的模式弹出窗口

    <div class="modal fade" id="delete-form" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
                          <div class="modal-dialog" role="document">
                            <div class="modal-content">
                              <div class="modal-header">
                                <h5 class="modal-title" id="exampleModalLabel">Delete Account</h5>
                                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                  <span aria-hidden="true">&times;</span>
                                </button>
                              </div>
                              <div class="modal-body">
    
                                <form id="delete-form" action="{{ route('account.destroy', [$account->id]) }}" method="post">
    
                                    <input type="hidden" name="_method" value="delete">
                                        {{ csrf_field() }}
                                        <p>Are you sure you want to delete this data? </p>
    
                              </div>
                              <div class="modal-footer">
                                <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                                <button type="submit" class="btn btn-primary">Save changes</button>
                              </div>
                              </form>
                            </div>
                          </div>
                        </div>
    

    还有我的销毁控制器

    public function destroy(Account $account)
        {
            $findAccount = Account::find($account->id);
    
            if($findAccount->delete()){
    
                return redirect()->route('account.index')->with('success', 'Account details delete successfully!');
            }
    
            return back()->withInput()->with('error', 'Account details could not be deleted.');
        }
    

    我无法删除表中的所有数据。他们保留一个数据,不能删除。

    5 回复  |  直到 6 年前
        1
  •  0
  •   Kamil    6 年前

    尝试以下操作:

    @extends('template.app')
    
    
            @section('content')
    
                <div class="col-md-9"> 
                                <!-- table content -->
    
                                <div class="card">
                                    <div class="card-header main-color-bg">
                                        <h3 class="card-title">Account Details</h3>
                                    </div>
                                    <div class="card-body">
                                        <div class="row">
                                            <div class="col-md-12">
                                                <input class="form-control rounded-corner" type="text" placeholder="Search Member Here" style="margin-bottom: 20px;">
                                            </div>
                                        </div>
    
                                        <!-- success message -->            
                                        @include('inc.message')
    
                                        <table class="table">
                                          <thead>
                                            <tr>
                                              <th scope="col">#</th>
                                              <th scope="col">Account Number</th>
                                              <th scope="col">Type</th>
                                              <th scope="col">Amount</th>
                                              <th scope="col">Action</th>
                                            </tr>
                                          </thead>
                                          <tbody>
    
                                        @if(is_empty($accounts))
            <tr><td>NO DATA</td></tr>
            @else
                                            @foreach($accounts as $account)
    
                                                <tr>                               
                                                  <th scope="row">{{$account->id}}</th>
                                                  <td>{{$account->acc_no}}</td>
                                                  <td>{{$account->acc_type}}</td>
                                                  <td>{{$account->amount}}</td>                             
                                                  <td><a class="btn btn btn-secondary" href="{{route('account.edit', $account->id)}}"><span class="fa fa-pencil"></span> Edit</a> <a class="btn btn btn-danger"
                                                  data-toggle="modal" data-target="#delete-form"><span class="fa fa-trash-o"></span> Delete</a></td>
                                                </tr>       
    
                                             @endforeach
                                         @endif
                                          </tbody>
                                        </table>
    
                                        <!-- pagination -->
    
                                            <nav id="pagination">                               
                                              <ul class="pagination justify-content-center">
                                                <li class="page-item disabled">
                                                  <span class="page-link"><span class="fa fa-arrow-circle-left"></span></span>
                                                </li>
                                                <li class="page-item"><a class="page-link" href="#">1</a></li>
                                                <li class="page-item active">
                                                  <span class="page-link">
                                                    2
                                                    <span class="sr-only">(current)</span>
                                                  </span>
                                                </li>
                                                <li class="page-item"><a class="page-link" href="#">3</a></li>
                                                <li class="page-item">
                                                  <a class="page-link" href="#"><span class="fa fa-arrow-circle-right"></span></a>
                                                </li>
                                              </ul>                               
                                            </nav>                          
    
                                        <!-- end pagination -->
                                        <button type="button" class="btn btn-success" data-toggle="modal" data-target="#myModal"><span class="fa fa-plus"></span> Add Account</button>
                                    </div>
                                </div>
    
                                <!-- end table content -->
                            </div>  
    
                                <!-- delete modal -->
    
                                <!-- Modal -->
                                <div class="modal fade" id="delete-form" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
                                  <div class="modal-dialog" role="document">
                                    <div class="modal-content">
                                      <div class="modal-header">
                                        <h5 class="modal-title" id="exampleModalLabel">Delete Account</h5>
                                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                          <span aria-hidden="true">&times;</span>
                                        </button>
                                      </div>
                                      <div class="modal-body">
    
                                        <form id="delete-form" action="{{ route('account.destroy', [$account->id]) }}" method="delete">
    
                                            <input type="hidden" name="_method" value="delete">
                                                {{ csrf_field() }}
                                                <p>Are you sure you want to delete this data? </p>
    
                                      </div>
                                      <div class="modal-footer">
                                        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                                        <button type="submit" class="btn btn-primary">Save changes</button>
                                      </div>
                                      </form>
                                    </div>
                                  </div>
                                </div>
    
                                <!-- end delete modal-->
    
                            <!-- modal popup -->
    
                            <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
                              <div class="modal-dialog" role="document">
                                <div class="modal-content">
                                  <div class="modal-header" style="background-color: #309fe2; color: #fff;">
                                    <h5 class="modal-title" id="exampleModalLabel">Account Details</h5>
                                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                      <span aria-hidden="true">&times;</span>
                                    </button>
                                  </div>
    
                                {!! Form::open(['url' => '/account', 'id' => 'eventForm', 'data-toggle' => 'validator', 'role' => 'form']) !!}
    
                                    {{ csrf_field() }}
                                  <div class="modal-body">
                                    <!-- modal form -->                          
    
    
    
                                          <div class="form-group">
                                            <label for="acc_no">Account Number</label>
                                            <input type="number" class="form-control" name="acc_no" id="acc_no" maxlength="20" placeholder="Enter your account number" required> 
                                          </div>
                                          <div class="form-group">
                                            <label for="name">Name</label>
                                            <input type="text" class="form-control" maxlength="30" name="name" id="name" placeholder="Enter your name" required> 
                                          </div>
                                          <div class="form-group">
                                            <label for="id_no">Identity No</label>
                                            <input type="number" class="form-control" maxlength="9" name="id_no" id="id_no" placeholder="Enter your identity no" required> 
                                          </div>
                                          <div class="form-group">
                                            <label for="bank_id">Bank</label>
                                            <select class="form-control" name="bank_id">
                                                @if(!empty($banks))
                                                    @foreach($banks as $bank)
                                                        <option value="{{ $bank->id }}">{{ $bank->name }}</option>
                                                    @endforeach
                                                @endif
                                            </select>
                                          </div>
                                            <div class="form-group">
                                            <label for="acc_type">Account Type</label>
                                            <select class="form-control" id="acc_type" name="acc_type">
                                                <option value="Saving">Saving</option>
                                                <option value="Current">Current</option>
                                                <option value="Deposite">Deposite</option>
                                            </select>
                                          </div>
                                            <div class="form-group">
                                            <label for="amount">Amount</label>
                                            <input type="text" pattern="[0-9.]" class="form-control" maxlength="15" name="amount" id="amount" placeholder="Enter your amount" required> 
                                          </div>                                                
    
                                    <!-- end modal form -->
                                  </div>
                                  <div class="modal-footer">
                                    <button type="button" class="btn btn-secondary" data-dismiss="modal"><span class="fa fa-times-circle"></span> Close</button>
                                    <button type="submit" class="btn btn-primary pull-right"><span class="fa fa-money"></span> Save changes</button>
                                  </div>
                                  {!! Form::close() !!} 
                                </div>
                              </div>
                            </div>
    
                            <!-- end modal -->
    
            @endsection
    
        2
  •  0
  •   Kamil    6 年前

    在删除表单上,将方法从POST更改为delete。

        3
  •  0
  •   Kevin Yan    6 年前

    我想可能是 Account::find($account->id); 未获取正确的帐户对象。使用 findOrFail 要再次拍摄:

    public function destroy(Account $account)
    {
        $findAccount = Account::findOrFail($account->id);
        ...
    }
    
        4
  •  0
  •   Kalana Mihiranga    6 年前

    这是我的表视图。我把if语句放在哪里

    @foreach($accounts as $account)
    
                                        <tr>                               
                                          <th scope="row">{{$account->id}}</th>
                                          <td>{{$account->acc_no}}</td>
                                          <td>{{$account->acc_type}}</td>
                                          <td>{{$account->amount}}</td>                             
                                          <td><a class="btn btn btn-secondary" href="{{route('account.edit', $account->id)}}"><span class="fa fa-pencil"></span> Edit</a> <a class="btn btn btn-danger"
                                          data-toggle="modal" data-target="#delete-form"><span class="fa fa-trash-o"></span> Delete</a></td>
                                        </tr>       
    
                                     @endforeach
    
        5
  •  0
  •   Kalana Mihiranga    6 年前
    @extends('template.app')
    
    
    @section('content')
    
        <div class="col-md-9"> 
                        <!-- table content -->
    
                        <div class="card">
                            <div class="card-header main-color-bg">
                                <h3 class="card-title">Account Details</h3>
                            </div>
                            <div class="card-body">
                                <div class="row">
                                    <div class="col-md-12">
                                        <input class="form-control rounded-corner" type="text" placeholder="Search Member Here" style="margin-bottom: 20px;">
                                    </div>
                                </div>
    
                                <!-- success message -->            
                                @include('inc.message')
    
                                <table class="table">
                                  <thead>
                                    <tr>
                                      <th scope="col">#</th>
                                      <th scope="col">Account Number</th>
                                      <th scope="col">Type</th>
                                      <th scope="col">Amount</th>
                                      <th scope="col">Action</th>
                                    </tr>
                                  </thead>
                                  <tbody>
    
                                @if(!empty($accounts))
                                    @foreach($accounts as $account)
    
                                        <tr>                               
                                          <th scope="row">{{$account->id}}</th>
                                          <td>{{$account->acc_no}}</td>
                                          <td>{{$account->acc_type}}</td>
                                          <td>{{$account->amount}}</td>                             
                                          <td><a class="btn btn btn-secondary" href="{{route('account.edit', $account->id)}}"><span class="fa fa-pencil"></span> Edit</a> <a class="btn btn btn-danger"
                                          data-toggle="modal" data-target="#delete-form"><span class="fa fa-trash-o"></span> Delete</a></td>
                                        </tr>       
    
                                     @endforeach
                                 @endif
                                  </tbody>
                                </table>
    
                                <!-- pagination -->
    
                                    <nav id="pagination">                               
                                      <ul class="pagination justify-content-center">
                                        <li class="page-item disabled">
                                          <span class="page-link"><span class="fa fa-arrow-circle-left"></span></span>
                                        </li>
                                        <li class="page-item"><a class="page-link" href="#">1</a></li>
                                        <li class="page-item active">
                                          <span class="page-link">
                                            2
                                            <span class="sr-only">(current)</span>
                                          </span>
                                        </li>
                                        <li class="page-item"><a class="page-link" href="#">3</a></li>
                                        <li class="page-item">
                                          <a class="page-link" href="#"><span class="fa fa-arrow-circle-right"></span></a>
                                        </li>
                                      </ul>                               
                                    </nav>                          
    
                                <!-- end pagination -->
                                <button type="button" class="btn btn-success" data-toggle="modal" data-target="#myModal"><span class="fa fa-plus"></span> Add Account</button>
                            </div>
                        </div>
    
                        <!-- end table content -->
                    </div>  
    
                        <!-- delete modal -->
    
                        <!-- Modal -->
                        <div class="modal fade" id="delete-form" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
                          <div class="modal-dialog" role="document">
                            <div class="modal-content">
                              <div class="modal-header">
                                <h5 class="modal-title" id="exampleModalLabel">Delete Account</h5>
                                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                  <span aria-hidden="true">&times;</span>
                                </button>
                              </div>
                              <div class="modal-body">
    
                                <form id="delete-form" action="{{ route('account.destroy', [$account->id]) }}" method="delete">
    
                                    <input type="hidden" name="_method" value="delete">
                                        {{ csrf_field() }}
                                        <p>Are you sure you want to delete this data? </p>
    
                              </div>
                              <div class="modal-footer">
                                <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                                <button type="submit" class="btn btn-primary">Save changes</button>
                              </div>
                              </form>
                            </div>
                          </div>
                        </div>
    
                        <!-- end delete modal-->
    
                    <!-- modal popup -->
    
                    <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
                      <div class="modal-dialog" role="document">
                        <div class="modal-content">
                          <div class="modal-header" style="background-color: #309fe2; color: #fff;">
                            <h5 class="modal-title" id="exampleModalLabel">Account Details</h5>
                            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                              <span aria-hidden="true">&times;</span>
                            </button>
                          </div>
    
                        {!! Form::open(['url' => '/account', 'id' => 'eventForm', 'data-toggle' => 'validator', 'role' => 'form']) !!}
    
                            {{ csrf_field() }}
                          <div class="modal-body">
                            <!-- modal form -->                          
    
    
    
                                  <div class="form-group">
                                    <label for="acc_no">Account Number</label>
                                    <input type="number" class="form-control" name="acc_no" id="acc_no" maxlength="20" placeholder="Enter your account number" required> 
                                  </div>
                                  <div class="form-group">
                                    <label for="name">Name</label>
                                    <input type="text" class="form-control" maxlength="30" name="name" id="name" placeholder="Enter your name" required> 
                                  </div>
                                  <div class="form-group">
                                    <label for="id_no">Identity No</label>
                                    <input type="number" class="form-control" maxlength="9" name="id_no" id="id_no" placeholder="Enter your identity no" required> 
                                  </div>
                                  <div class="form-group">
                                    <label for="bank_id">Bank</label>
                                    <select class="form-control" name="bank_id">
                                        @if(!empty($banks))
                                            @foreach($banks as $bank)
                                                <option value="{{ $bank->id }}">{{ $bank->name }}</option>
                                            @endforeach
                                        @endif
                                    </select>
                                  </div>
                                    <div class="form-group">
                                    <label for="acc_type">Account Type</label>
                                    <select class="form-control" id="acc_type" name="acc_type">
                                        <option value="Saving">Saving</option>
                                        <option value="Current">Current</option>
                                        <option value="Deposite">Deposite</option>
                                    </select>
                                  </div>
                                    <div class="form-group">
                                    <label for="amount">Amount</label>
                                    <input type="text" pattern="[0-9.]" class="form-control" maxlength="15" name="amount" id="amount" placeholder="Enter your amount" required> 
                                  </div>                                                
    
                            <!-- end modal form -->
                          </div>
                          <div class="modal-footer">
                            <button type="button" class="btn btn-secondary" data-dismiss="modal"><span class="fa fa-times-circle"></span> Close</button>
                            <button type="submit" class="btn btn-primary pull-right"><span class="fa fa-money"></span> Save changes</button>
                          </div>
                          {!! Form::close() !!} 
                        </div>
                      </div>
                    </div>
    
                    <!-- end modal -->
    
    @endsection