Php 从表中删除项目-Laravel
在下面的代码中,我在一个表中显示学生列表。现在,当我删除表中的最后一个学生时,它会删除第一个人。为什么会这样?可能是我没有很好地循环来获取每个学生的id吗 单击delete按钮时,会弹出一个模式按钮,我单击yes以执行以下删除操作 附言:拉威尔初学者 控制器Php 从表中删除项目-Laravel,php,laravel,Php,Laravel,在下面的代码中,我在一个表中显示学生列表。现在,当我删除表中的最后一个学生时,它会删除第一个人。为什么会这样?可能是我没有很好地循环来获取每个学生的id吗 单击delete按钮时,会弹出一个模式按钮,我单击yes以执行以下删除操作 附言:拉威尔初学者 控制器 public function index() { $students= Student::where('user_id', Auth::user()->id)->get(); retu
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>
<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>
@foreach(学生为$std)
{{$std->name}
{{$std->phone}
不
@否则
@恩迪夫
$('#myModal').on('show.bs.modal',函数(e){
var$modal=$(本);
var userId=e.relatedTarget.dataset.uid;
//您可以将用户ID为的url添加到link href属性
$modal.find('.modal footer a.red-text').attr('href','/customer/'+userId+'/delete');
})
如果你做得不对,你必须为每个删除锚指定std->id
,并给锚指定一个idid=“del-anchor”
比如
<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>
它将删除相应的学生,您还需要调用
$(“#myModal”)。删除成功时隐藏 在您的情况下,最佳做法是在单击“删除”按钮时将用户ID发送到引导模式
首先,在删除按钮上添加一个数据属性
,以传递用户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>
因此,您确定用于删除的ID将是正确的。
希望这对你有帮助:)
更新:
我制作了一个简单的代码笔来说明这一点
您有一个模式窗口,其中有一个要删除的链接。为每个学生创建一个模式,或者动态地将数据传递给模式。您在传递数据时不会向模式传递任何数据opens@u_mulder我试着用@foreach($students as$std)在模式上循环(动态传递数据),但没有成功。检查我的更新。我将href放在模式中为'/student/'+userId+'/delete'
,但它不是work@JonathanFeud查看我的代码笔示例,它可以帮助您;)
$(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();
}
<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>
$('#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');
})