php - 从表中删除项目 - Laravel

标签 php laravel

在下面的代码中,我在表格中显示学生列表。现在,当我删除表中的最后一个学生时,它会删除第一个人。为什么会发生这种情况?难道是我没有很好地循环获取每个学生的 id?

当单击删除按钮时,会弹出一个模式按钮,我单击"is"即可执行删除操作,如下所示

PS:Laravel 初学者

Controller

 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>

最佳答案

就您而言,最佳实践是在单击删除按钮时将用户 ID 发送到 Bootstrap 模式。

首先,在删除按钮上添加数据属性以传递用户 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

关于php - 从表中删除项目 - Laravel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49892619/

相关文章:

laravel - 按 laravel lighthouse graphql 中的相关列排序

php - Redis - 连接被拒绝

php - 无法添加或更新子行,外键约束失败

php - laravel Eloquent : filtering rows with month or date

database - Laravel SelectRaw 与数据库 :Raw

javascript - 当我来自两个不同的函数时变量未定义

javascript - 数据库查询的结果未显示在我的页面上

php - MySQL 不使用 INNER JOIN 在单独的表中显示重复的列名

php - 从数据库中获取数据并在php中的邮件功能中使用

php - Laravel Horizo​​n Assets 中出现 404