如何从 table 中检索文件 ID 并将其传递给 Laravel 5 中的模态?
How to retrieve file id from table and pass it to modal in Laravel 5?
在我的项目中,我有一个 table,它列出了用户上传的所有文件以及查看或删除它们的选项。当用户单击删除时,它会打开一个模式,要求确认。一切正常,除了当我在第一个文件以外的任何其他文件中单击删除时,模式总是要求确认删除第一个文件。简而言之,只有 table 中的第一个文件被删除。我无法检索其他文件的 ID,无法将其传递给 deleteentry/{id}。
这是带有模态 (myModal) 的代码:
<tbody class="searchable">
@foreach ($files as $entry)
<tr>
<td><b>{{ $entry->original_filename }}</b></td>
<td><b>{{ $entry->created_at }}</b></td>
<td><a href="{{ route('getentry', [$entry->filename]) }}" class="btn btn-flat btn-primary">View</a></td>
<td>
<a href="#myModal" data-toggle="modal" class="btn btn-flat btn-danger">Delete</a>
<!-- Modal HTML -->
<div id="myModal" class="modal fade">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title" style="color:IndianRed;">Confirmation</h4>
</div>
<hr>
<div class="modal-body" style="margin-top:-20px;">
<p>Do you want to delete the file {{ $entry->original_filename }} </a>permanently?</p>
<p class="text-primary" style="margin-top:-40px;"><small>If you click yes, the file will be deleted from our database permanently.</small></p>
</div>
<div class="modal-footer" style="margin-top:-40px;">
<button type="button" class="btn btn-flat btn-primary" data-dismiss="modal">No</button>
<a href="{{ route('deleteentry', [$entry->id]) }}" class="btn btn-danger btn-flat" style="width:90px;" data-toggle="modal">Yes</a>
</div>
</div>
</div>
</div>
</td>
</tr>
@endforeach
</tbody>
当我完全删除模态并且只有:
<tbody class="searchable">
@foreach ($files as $entry)
<tr>
<td><b>{{ $entry->original_filename }}</b></td>
<td><b>{{ $entry->created_at }}</b></td>
<td><a href="{{ route('getentry', [$entry->filename]) }}" class="btn btn-flat btn-primary">View</a></td>
<td>
<a href="{{ route('deleteentry', [$entry->id]) }}" class="btn btn-flat btn-danger">Delete</a>
</td>
</tr>
@endforeach
</tbody>
所以这表明问题出在模态上,但我想不出解决办法。
您对所有确认模式的 id
属性使用相同的值,这是不允许的。因此,为避免这种情况,您需要向每个项目的 id
属性添加一些额外的数据。确保它们唯一的最简单方法是将 $entry->id
附加到每个,如下所示:
<tbody class="searchable">
@foreach ($files as $entry)
<tr>
<td><b>{{ $entry->original_filename }}</b></td>
<td><b>{{ $entry->created_at }}</b></td>
<td><a href="{{ route('getentry', [$entry->filename]) }}" class="btn btn-flat btn-primary">View</a></td>
<td>
<a href="#deleteModal_{{ $entry->id }}" data-toggle="modal" class="btn btn-flat btn-danger">Delete</a>
<!-- Modal HTML -->
<div id="deleteModal_{{ $entry->id }}" class="modal fade">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title" style="color:IndianRed;">Confirmation</h4>
</div>
<hr>
<div class="modal-body" style="margin-top:-20px;">
<p>Do you want to delete the file {{ $entry->original_filename }} </a>permanently?</p>
<p class="text-primary" style="margin-top:-40px;"><small>If you click yes, the file will be deleted from our database permanently.</small></p>
</div>
<div class="modal-footer" style="margin-top:-40px;">
<button type="button" class="btn btn-flat btn-primary" data-dismiss="modal">No</button>
<a href="{{ route('deleteentry', [$entry->id]) }}" class="btn btn-danger btn-flat" style="width:90px;" data-toggle="modal">Yes</a>
</div>
</div>
</div>
</div>
</td>
</tr>
@endforeach
</tbody>
在我的项目中,我有一个 table,它列出了用户上传的所有文件以及查看或删除它们的选项。当用户单击删除时,它会打开一个模式,要求确认。一切正常,除了当我在第一个文件以外的任何其他文件中单击删除时,模式总是要求确认删除第一个文件。简而言之,只有 table 中的第一个文件被删除。我无法检索其他文件的 ID,无法将其传递给 deleteentry/{id}。
这是带有模态 (myModal) 的代码:
<tbody class="searchable">
@foreach ($files as $entry)
<tr>
<td><b>{{ $entry->original_filename }}</b></td>
<td><b>{{ $entry->created_at }}</b></td>
<td><a href="{{ route('getentry', [$entry->filename]) }}" class="btn btn-flat btn-primary">View</a></td>
<td>
<a href="#myModal" data-toggle="modal" class="btn btn-flat btn-danger">Delete</a>
<!-- Modal HTML -->
<div id="myModal" class="modal fade">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title" style="color:IndianRed;">Confirmation</h4>
</div>
<hr>
<div class="modal-body" style="margin-top:-20px;">
<p>Do you want to delete the file {{ $entry->original_filename }} </a>permanently?</p>
<p class="text-primary" style="margin-top:-40px;"><small>If you click yes, the file will be deleted from our database permanently.</small></p>
</div>
<div class="modal-footer" style="margin-top:-40px;">
<button type="button" class="btn btn-flat btn-primary" data-dismiss="modal">No</button>
<a href="{{ route('deleteentry', [$entry->id]) }}" class="btn btn-danger btn-flat" style="width:90px;" data-toggle="modal">Yes</a>
</div>
</div>
</div>
</div>
</td>
</tr>
@endforeach
</tbody>
当我完全删除模态并且只有:
<tbody class="searchable">
@foreach ($files as $entry)
<tr>
<td><b>{{ $entry->original_filename }}</b></td>
<td><b>{{ $entry->created_at }}</b></td>
<td><a href="{{ route('getentry', [$entry->filename]) }}" class="btn btn-flat btn-primary">View</a></td>
<td>
<a href="{{ route('deleteentry', [$entry->id]) }}" class="btn btn-flat btn-danger">Delete</a>
</td>
</tr>
@endforeach
</tbody>
所以这表明问题出在模态上,但我想不出解决办法。
您对所有确认模式的 id
属性使用相同的值,这是不允许的。因此,为避免这种情况,您需要向每个项目的 id
属性添加一些额外的数据。确保它们唯一的最简单方法是将 $entry->id
附加到每个,如下所示:
<tbody class="searchable">
@foreach ($files as $entry)
<tr>
<td><b>{{ $entry->original_filename }}</b></td>
<td><b>{{ $entry->created_at }}</b></td>
<td><a href="{{ route('getentry', [$entry->filename]) }}" class="btn btn-flat btn-primary">View</a></td>
<td>
<a href="#deleteModal_{{ $entry->id }}" data-toggle="modal" class="btn btn-flat btn-danger">Delete</a>
<!-- Modal HTML -->
<div id="deleteModal_{{ $entry->id }}" class="modal fade">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title" style="color:IndianRed;">Confirmation</h4>
</div>
<hr>
<div class="modal-body" style="margin-top:-20px;">
<p>Do you want to delete the file {{ $entry->original_filename }} </a>permanently?</p>
<p class="text-primary" style="margin-top:-40px;"><small>If you click yes, the file will be deleted from our database permanently.</small></p>
</div>
<div class="modal-footer" style="margin-top:-40px;">
<button type="button" class="btn btn-flat btn-primary" data-dismiss="modal">No</button>
<a href="{{ route('deleteentry', [$entry->id]) }}" class="btn btn-danger btn-flat" style="width:90px;" data-toggle="modal">Yes</a>
</div>
</div>
</div>
</div>
</td>
</tr>
@endforeach
</tbody>