如果我在foreach中循环了一些结果,那么列表就是这样 -
-item 1
delete
-item 2
delete
-item 3
delete
-item 4
delete
如果我想删除item 3
记录,如何为查询找到该迭代的正确ID。通常我会使用get请求,但在这种情况下不会起作用。
由于
@foreach ($statuses as $status)
<div class="media post-margin">
<a class="pull-left" href="{{ route('profile.index', ['username' => $status->user->username]) }}">
<img class="media-object" alt="{{ $status->user->getNameOrUsername() }}" src="{{ $status->user->getAvatarUrl() }}">
</a>
<div class="media-body">
<h4 class="media-heading"><a href="{{ route('profile.index', ['username' => $status->user->username]) }}">{{ $status->user->getNameOrUsername() }}</a></h4>
<span>{{ $status->created_at->diffForHumans() }}</span>
</div>
<p>{{ $status->body }}</p>
<hr>
<ul class="list-inline">
<li><a href="{{ route('status.like', ['statusId' => $status->id]) }}"><i class="fa fa-thumbs-up fa-liked"></i> {{ $status->likes->count() }} {{ str_plural('like', $status->likes->count()) }}</a></li>
<li><a href="#"><i class="fa fa-comments-o"></i> Comment</a></li>
<li><a href="#"><i class="fa fa-retweet"></i> Share</a></li>
@if ($status->user->id === Auth::user()->id)
<li id="remove"><a href="#"><i class="fa fa-times"></i> Delete</li>
@endif
</ul>
<hr>
</div>
@endforeach
答案 0 :(得分:0)
您只需传递要删除的记录ID即可。
@foreach ($statuses as $status)
<div class="media post-margin">
<a class="pull-left" href="{{ route('profile.index', ['username' => $status->user->username]) }}">
<img class="media-object" alt="{{ $status->user->getNameOrUsername() }}" src="{{ $status->user->getAvatarUrl() }}">
</a>
<div class="media-body">
<h4 class="media-heading"><a href="{{ route('profile.index', ['username' => $status->user->username]) }}">{{ $status->user->getNameOrUsername() }}</a></h4>
<span>{{ $status->created_at->diffForHumans() }}</span>
</div>
<p>{{ $status->body }}</p>
<hr>
<ul class="list-inline">
<li><a href="{{ route('status.like', ['statusId' => $status->id]) }}"><i class="fa fa-thumbs-up fa-liked"></i> {{ $status->likes->count() }} {{ str_plural('like', $status->likes->count()) }}</a></li>
<li><a href="#"><i class="fa fa-comments-o"></i> Comment</a></li>
<li><a href="#"><i class="fa fa-retweet"></i> Share</a></li>
@if ($status->user->id === Auth::user()->id)
<li id="remove"><a href="{{route('status.delete',['statusId',$status -> id])}}"><i class="fa fa-times"></i> Delete</li>
@endif
</ul>
<hr>
</div>
@endforeach
路线
route::get('/delete/{$id}',['as' =>'status.delete' , 'uses' => 'YourController@delete']);
控制器
public function delete($id){
Model::destroy($id); //
}
希望这有帮助。