【问题标题】:Beginner in Laravel Delete data using modalLaravel 初学者使用模态删除数据
【发布时间】:2020-10-11 10:27:52
【问题描述】:

我的模式运行良好,可以删除数据,但不能删除我选择删除的确切 ID,例如 id: 1, 2, 3, 4,我将选择 id: 4 但 id: 4 不会删除 id删除的是 id: 1

这是我在 AllSystemController 中的删除函数

public function deletestorydata($id)
{
    $story = Story::findOrFail($id);
    $story->delete();
    return redirect()->back()->with('success', 'Data has successfully deleted.');
}

这是我在故事部分的路线 web.php

Route::get('stories/table', 'AllSystemController@indexstoriesdata')->name('stories.table');
Route::get('/stories/add', 'AllSystemController@createviewstories')->name('create.stories');
Route::post('/stories/storiesdata/submit', 'AllSystemController@submitstories')->name('submit.stories.data');
Route::get('/stories/view/{id}', 'AllSystemController@showviewstories')->name('view.story.data');
Route::get('/stories/edit/{id}', 'AllSystemController@editviewstories')->name('edit.stories');
Route::patch('/stories/{id}', 'AllSystemController@updatestorydata')->name('update.story.data');
Route::delete('/deletestory/{id}', 'AllSystemController@deletestorydata')->name('delete.story.data');

这是我的观点 index_stories_data.blade.php

<table id="table-style-hover" class="table table-striped table-hover table-bordered nowrap">
<thead>
<tr>
    <th width="1%">Image</th> 
    <th>Title</th>
    <th>Flagship</th>
    <th>Category</th>
    <th>Author</th>
    <th class="text-center">Action</th>
</tr>
</thead>
<tbody>
@foreach($story as $row)
<tr>
    <td><img src="{{ asset('storage/stories_images')}}/{{$row->display_image}}" class="img-thumbnail" width="50"></td>
    <td>{{$row->title}}</td>
    <td>{{$row->is_flagship}}</td>
    <td>{{$row->category_name}}</td>
    <td>{{$row->name}}</td>
    <td>
    <div class="dropdown">
       <button class="btn btn-primary" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Action&nbsp;<i class="fa fa-sort-desc" aria-hidden="true"></i></button>
  <div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
    <a style="color:black; font-weight: bold;" class="dropdown-item" href="{{url('/stories/view', $row->id)}}" target="_blank">View</a>
    <a style="color:black; font-weight: bold;" class="dropdown-item" href="{{url('/stories/edit', $row->id)}}" target="_blank" class="btn btn-primary">Edit</a>
    <a style="color:black; font-weight: bold;" class="dropdown-item" data-toggle="modal" data-target="#delete" href="">Delete</a>

  </div>
</div>

这是我的模式和脚本

<div class="modal fade modal-icon" id="delete" tabindex="-1" role="dialog" aria- 
labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">

<h4 class="modal-title" id="myModalLabel">Delete Confirmation</h4>
</div>
<form method="post" class="delete_form" action="{{url('/deletestory', $row->id)}}">
    {{csrf_field()}}
    <input type="hidden" name="_method" value="DELETE">
<div class="modal-body">
  <h4 class="text-center">Are you sure you want to delete it?</h4>
</div>

<div class="modal-footer">
<button type="button" class="btn btn-process" data-dismiss="modal">Cancel</button>
<button type="submit" class="btn btn-danger">Confirm</button>
</form>

</div>
</div>
</div>
</div>

<script>
  $(document).ready(function(){
    $('.delete_form').on('delete', function(){
      if(confirm("Are you sure you want to delete this data?"))
      {
        return true;
      }
      else
      {
        return false;
      }
    });
  });
</script>
@endforeach
</tbody>
</table>

我试图 dd($story);值,例如 id: 1, 2, 3, 4,我会选择要删除的按钮是 id: 3 并且 dd 中的结果是 id: 1 我的函数有问题吗?我该如何解决这个问题?

【问题讨论】:

  • 在模态上下文中你的$row-&gt;id 是什么?
  • value inside story table,我在视图中检索数据 @foreach($story as $row)--@endforeach 这就是我在模态上下文中使用 $row->id 的原因
  • 能否在您的问题中分享刀片的 foreach 循环,以便我给您答案
  • 你想看的所有刀片?
  • @John only foreach 循环,foreachendforeach

标签: laravel laravel-5


【解决方案1】:

您是否有 js 函数来根据您要删除的项目更改表单操作中项目的 ID?如果不是,您的 id 将始终为 1 或数据中第一个对象的任何 id。

<form method="post" class="delete_form" action="{{url('/deletestory', $row->id)}}">

【讨论】:

  • 这是我的 js 脚本
  • 从您对主帖的评论中,您说您在模态之间使用 foreach 和 endforeach 这意味着您将拥有与 @story 对象中的项目一样多的模态,如果我理解这一点我的问题你会叫正确的模态吗?也许尝试单击删除按钮并使用检查工具查看它是否是您要删除的正确 ID。
  • 您是否检查了单击删除按钮时弹出的模态表单 action="{{url('/deletestory', $row->id)}}" 中的 id,如果它与您单击的删除按钮中的 id 匹配吗?因为正如在您的代码中看到的那样,您调用 id 为“delete”的模态,但如果您在模态之间使用 foreach,您将拥有许多 id 为“delete”的模态,并且它总是会调用第一个模态,其 id 为“删除”。
  • 我没有,我解决了我写了脚本来更改 id 如果我要点击删除按钮,谢谢你的想法......
【解决方案2】:

我编辑你的完整代码,不要在你的 foreach 中使用你的 js。

试试这个:

<table id="table-style-hover" class="table table-striped table-hover table-bordered nowrap">

 <thead>
  <tr>
    <th width="1%">Image</th> 
    <th>Title</th>
    <th>Flagship</th>
    <th>Category</th>
    <th>Author</th>
    <th class="text-center">Action</th>
  </tr>
 </thead>

  <tbody>

  @foreach($story as $row)
    <tr>
      <td><img src="{{ asset('storage/stories_images')}}/{{$row->display_image}}" class="img-thumbnail" width="50"></td>
      <td>{{$row->title}}</td>
      <td>{{$row->is_flagship}}</td>
      <td>{{$row->category_name}}</td>
      <td>{{$row->name}}</td>
      <td>
        <div class="dropdown">
             <button class="btn btn-primary" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Action&nbsp;<i class="fa fa-sort-desc" aria-hidden="true"></i></button>
          <div class="dropdown-menu" aria-labelledby="dropdownMenuButton">

            <a style="color:black; font-weight: bold;" class="dropdown-item" href="{{ url('/stories/view', $row->id)}}" target="_blank">View</a>

            <a style="color:black; font-weight: bold;" class="dropdown-item" href="{{url('/stories/edit', $row->id)}}" target="_blank" class="btn btn-primary">Edit</a>
            <!-- Delete method -->
            <a href="#" style="color:black; font-weight: bold;" data-href="{{ url('/deletestory', $row->id) }}" class="btn btn-info btn-outline btn-circle btn-lg" data-toggle="modal" data-target="#myModal" title="Delete">Delete</a>

          </div>
        </div>
      </td>
    </tr>
 @endforeach

  </tbody>
</table>

<!-- Delete Modal -->
<div class="container">
  <div class="modal fade" id="myModal" role="dialog">
    <div class="modal-dialog">
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal">&times;</button>
          <h4 class="modal-title">Delete !!!</h4>
        </div>
        <div class="modal-body text-center">
          <p class="my-0 font-weight-bold">Are you sure you want to delete this data???</p>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
          <a class="btn btn-danger btn-ok">Delete</a>
        </div>
      </div>
    </div>
  </div>
</div>


<!-- Delete Modal JS -->
<script>
  $('#myModal').on('show.bs.modal', function(e) {
      $(this).find('.btn-ok').attr('href', $(e.relatedTarget).data('href'));
  });
</script>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-07-14
    • 2020-08-10
    • 2020-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-10
    • 2016-10-09
    相关资源
    最近更新 更多