Beginner in Laravel Delete data using modal

我的未来我决定 提交于 2021-02-02 10:00:01

问题


My modal works so well and can delete data but not the exact id that i selected to delete like example id: 1, 2, 3, 4 and i will select id: 4 but the id: 4 will not delete the id deleted is id: 1

this is my delete function inside AllSystemController

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

and this is my route web.php in story section

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

and this is my view 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>

this is my modal and script

<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>

and i tried to dd($story); value, example id: 1, 2, 3, 4, and i will select button to delete is id: 3 and the result in dd is id: 1 is there something wrong to my function? how can i fix this?


回答1:


Do you have js function to change the id of the item in your form action accordingly to the item that you want to delete? If not your id will always be 1 or whatever id of the first object in your data.

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



回答2:


I edit your full code, don't use your js in your foreach.

Try this :

<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>


来源:https://stackoverflow.com/questions/62499542/beginner-in-laravel-delete-data-using-modal

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!