从表中删除项目 - Laravel

时间:2018-04-18 06:27:57

标签: php laravel

在下面的代码中,我在表格中显示学生列表。现在当我删除表中的最后一个学生时,它会删除第一个人。为什么会这样?难道我不能很好地获得每个学生的身份吗?

单击删除按钮时,会弹出一个模态按钮,然后单击是以执行下面的删除

PS:Laravel初学者

控制器

 public function index()
    {
        $students= Student::where('user_id', Auth::user()->id)->get();
        return view('students.index',compact('students'));
    }

查看

<tbody>
                      @foreach($students as $std)
                          <tr>
                           <td>{{$std->name}}</td>
                           <td>{{$std->phone}}</td>
                           <td>
                            <a  style="color:#000" href="/student/{{$std->id}}/edit" title="edit" ><i  style="font-size:16px" class="fa fa-edit"></i></a>
                        &nbsp;&nbsp;

             <a style="color:#000" data-toggle="modal" data-target="#myModal" title="delete" ><i style="font-size:16px"  class="fa fa-trash"></i></a>
                            </td>

                          </tr>
                          @endforeach
                        </tbody>


    <div class="modal fade" id="myModal" role="dialog">
        <div class="modal-dialog">
      <!-- Modal content-->

      <div class="modal-content">
            <div class="modal-header">
              <h4 class="modal-title">Warning</h4>
            </div>
            <div class="modal-body">
            <p>Do you wish to delete this student ?</p>
            </div>
            <div class="modal-footer">
            @if(!empty($std))
            <a href="/student/{{$std->id}}/delete" class=" modal-action waves-effect waves-light btn-flat red-text">Yes</a>
                    <a  data-dismiss="modal" class=" modal-action modal-close waves-effect waves-light green white-text btn">No</a>      
            @else     
            @endif
           </div>
          </div>

        </div>
      </div>

    </div>

更新

<div class="modal fade" id="myModal" role="dialog">
        <div class="modal-dialog">
      <!-- Modal content-->

      <div class="modal-content">
            <div class="modal-header">
              <h4 class="modal-title">Warning</h4>
            </div>
            <div class="modal-body">
            <p>Do you wish to delete this student ?</p>
            </div>
            <div class="modal-footer">
            @if(!empty($std))
            <a href="/student/+userId+/delete" class=" modal-action waves-effect waves-light btn-flat red-text">Yes</a>
                    <a  data-dismiss="modal" class=" modal-action modal-close waves-effect waves-light green white-text btn">No</a>      
            @else     
            @endif
           </div>
          </div>


<script>
$('#myModal').on('show.bs.modal', function(e) {
    var $modal = $(this);
    var userId = e.relatedTarget.dataset.uid;

    // You can add the url with user ID to link href attribute
    $modal.find('.modal-footer a.red-text').attr('href', '/customer/' + userId + '/delete');
})
</script>

2 个答案:

答案 0 :(得分:2)

在您的情况下,最佳做法是在您单击删除按钮时将用户ID发送到Bootstrap模式。

首先,在删除按钮上添加data-attribute以传递用户ID

<table>
    <tbody>
        @foreach($students as $std)
        <tr>
            <td>{{$std->name}}</td>
            <td>{{$std->phone}}</td>
            <td>
                <a style="color:#000" href="/student/{{$std->id}}/edit" title="edit">
                    <i style="font-size:16px" class="fa fa-edit"></i>
                </a>
                <a style="color:#000" data-toggle="modal" data-target="#myModal" data-uid="{{$std->id}}" title="delete">
                    <i style="font-size:16px" class="fa fa-trash"></i>
                </a>
            </td>
        </tr>
        @endforeach
    </tbody>
</table>

然后你需要添加一些jQuery来检索你的uid参数

$('#myModal').on('show.bs.modal', function(e) {
    var $modal = $(this);
    var userId = e.relatedTarget.dataset.uid;

    // You can add the url with user ID to link href attribute
    $modal.find('.modal-footer a.red-text').attr('href', '/student/' + userId + '/delete');
})

因此,您确定用于删除的ID将是正确的 希望这可以帮助你:)

更新:

我制作了一个简单的编码器来说明这一点 Codepen example

答案 1 :(得分:0)

你是以错误的方式做的,你必须为每个删除锚分配std->id并给id="del-anchor"一个id {/ p>}

<a   id='del-anchor' std-id={{$std->id}} style="color:#000" data-toggle="modal" data-target="#myModal" title="delete" ><i style="font-size:16px"  class="fa fa-trash"></i></a>

您还需要将id分配给模态锚并动态更改它。

$(document).on('click', '#del-anchor', function () {
   var std-id = $(this).attr('std-id');
   var href = "/student/"+std-id+"/delete";
   $("your-model-anchor").attr("href",href);
   $('#myModal').modal();
 }

它将删除相应的学生,删除成功时还需要致电$("#myModal").hide