Php 如何从数据库中删除注释?
我正在尝试从我的管理面板中删除评论。我有一个删除评论的功能,但我不知道为什么这不起作用 这是我的控制器:Php 如何从数据库中删除注释?,php,database,laravel,Php,Database,Laravel,我正在尝试从我的管理面板中删除评论。我有一个删除评论的功能,但我不知道为什么这不起作用 这是我的控制器: $article = \DB::table("events") ->where("id", $id) ->select("id", "subject", "information", "public", "category_id", "event_type_id", "country", "address", "city", "starts", "ends", "
$article = \DB::table("events")
->where("id", $id)
->select("id", "subject", "information", "public", "category_id", "event_type_id", "country", "address", "city", "starts", "ends", "organizer", "website", "email", "telephone")
->first();
$data['article'] = $article;
$event_comm = EventComment::where('event_id', $id)->get();
return view("admin.editEvent", $data)
->with(compact('event_comm'));
public function deleteComment($type, $id)
{
if($type == "Event")
{
$comment = \App\EventComment::find($id);
}
if($type == "Opinion")
{
$comment = \App\OpinionComment::find($id);
}
$comment->delete();
return redirect('admin/comments');
}
我的删除评论功能:
$article = \DB::table("events")
->where("id", $id)
->select("id", "subject", "information", "public", "category_id", "event_type_id", "country", "address", "city", "starts", "ends", "organizer", "website", "email", "telephone")
->first();
$data['article'] = $article;
$event_comm = EventComment::where('event_id', $id)->get();
return view("admin.editEvent", $data)
->with(compact('event_comm'));
public function deleteComment($type, $id)
{
if($type == "Event")
{
$comment = \App\EventComment::find($id);
}
if($type == "Opinion")
{
$comment = \App\OpinionComment::find($id);
}
$comment->delete();
return redirect('admin/comments');
}
删除评论的路由
Route::get('admin/article/deleteComment/{type?}/{id?}', 'ArticleController@deleteComment');
我的按钮
<button href="{{ url('admin/article/deleteComment/'.$article['type'].'/'.$article['id']) }}" role="button" class="btn btn-xs btn-danger" onclick="return confirm('Are you sure you want to delete this comment?');">Delete <i class="fa fa-trash"></i></button>
删除
试试这个
{!! Form::open(['method' => 'DELETE', 'route'=>['comments.destroy', $comment->id], 'style'=> 'display:inline', 'onsubmit' => 'return confirm("Are you sure you want to delete?")']) !!}
{!! Form::button('<i class="fa fa-trash"></i>',['type'=>'submit', 'class'=> 'btn btn-danger']) !!}
{!! Form::close() !!}
你应该试试这个:
请像这样改变你的路线
Route::get('admin/article/deleteComment/{type}/{id}', 'ArticleController@deleteComment')->name('commentdelete');
您的按钮如下所示:
<button href="{{ route('commentdelete',[$article['type'],$article['id']]) }}" role="button" class="btn btn-xs btn-danger" onclick="return confirm('Are you sure you want to delete this comment?');">Delete <i class="fa fa-trash"></i></button>
删除
是否传递注释id。。?或者事件id?{type?}/{id?}
-为什么要打问号?查看控制器,这两个参数都是必需的。那么我应该删除问号吗?在尝试删除之前,您也不会检查是否确实收到了注释。如果有人传入了无效的类型或id,您将无法得到回复。进行一些调试,以查看是否所有值都是您期望的值,并且是否定义了$comment
。问号用于可选参数。因此,如果您访问admin/article/deletecoment/
,您将得到一个错误,因为控制器需要两个参数,但不会得到任何参数。一个好的答案包括解释OP为什么应该“尝试这个”。你改变了什么?为什么?此外,查看代码时,OP还需要type
-值来知道要删除的注释类型。感谢您的回答。但暂时不工作,当我按下编辑按钮时,我的模式无法打开以查看要编辑的字段。下面是我的想法:谢谢你的回答,先生。你知道为什么我按编辑按钮时我的模式没有打开吗?没有href=“{{route('commentdelete',[$article['type'],$article['id']])}”
它在工作,但我需要它,如何正确使用?我正在使用laravel 5.2是的,这是laravel的正确方法,您可以在锚标记中使用url和路由名称如果我的答案对您有效,请接受并向上投票给我的答案请稍等,你知道为什么我的模式现在没有打开吗?这里是我的编辑模式的代码:
,因为当你在模式href中创建任何路由时,然后当你点击按钮时,他们会在路由上重定向,所以当你使用href作为弹出模式时,请只写href=“#”