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

cannot delete last data on table using laravel destroy function

我创建了一个 laravel 销毁函数,并使用模态删除数据。但我无法删除数据库中的所有数据。最后一个不能删除。我认为这是有原因的。

这是我在视图中的模态弹出窗口

<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.');
    }

我无法删除 table 中的所有数据。他们保留一份数据,无法删除。

在您的删除表单中,将方法从 POST 更改为 DELETE。

我想可能是Account::find($account->id);没有得到正确的Account对象。使用 findOrFail 再拍一次:

public function destroy(Account $account)
{
    $findAccount = Account::findOrFail($account->id);
    ...
}

这是我的 table 观点。我把 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
@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

试试这个:

@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