我想删除状态评论
我的route
:
Route::get('/delete-comment/{id}/', [
'uses' => 'classroom@getDeleteComment',
'as' => 'Comment.delete',
'middleware' => 'auth'
]);
我的controller
:
public function getDeleteComment(Request $request,$id)
{
$class_id = $request->get('class_id');
$comment = StatusComments::where('id', $id)->first();
$comment->delete();
Flash::success('Successfuly deleted your comment!');
return redirect(route('class',['class_id'=>$class_id]));
}
我的删除链接:
<a href="{{ route('Comment.delete', ['id' => $comment->id]) }}" ><i class="fa fa-trash"></i></a>
当我删除点击选项时,它会给我错误。我的链接genarate http://localhost:8000/class但是应该来http://localhost:8000/class/69。其中69是由教室表id生成的页面链接。这就是我已经在状态表中插入class_id字段的原因。
答案 0 :(得分:1)
首先更改您的路线以包含class_id
:
Route::get('/{class_id}/delete-comment/{id}/', [
'uses' => 'classroom@getDeleteComment',
'as' => 'Comment.delete',
'middleware' => 'auth'
]);
其次,更改您的删除链接,如下所示:
<a href="{{ route('Comment.delete', ['class_id'=>$class_id,'id' => $comment->id]) }}" ><i class="fa fa-trash"></i></a>
最后,将class_id
参数添加到getDeleteComment
方法:
public function getDeleteComment(Request $request,$class_id,$id)
{
$comment = StatusComments::where('id', $id)->first();
$comment->delete();
Flash::success('Successfuly deleted your comment!');
return redirect(route('class',['class_id'=>$class_id]));
}