删除工作但不重定向laravel 5.2中的链接页面

时间:2016-09-13 18:27:04

标签: laravel-5.2

我想删除状态评论 我的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字段的原因。

1 个答案:

答案 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]));
    }