2 回答
TA贡献1887条经验 获得超5个赞
尝试此操作以限制仅对拥有评论的经过身份验证的用户进行删除:
/**
* Comments Controller Method Delete
*/
public function delete($id){
if(!DB::table('comments')->where('id',$id)->where('user_id',auth()->user()->id)->delete()){
Session::flash('remove', "You do not have permission to delete the comment!");
}else{
Session::flash('remove', "Message removed successfully!");
}
return Redirect::back();
}
对于您的第二个问题,我认为会发生的情况是,如果没有评论,您正在使用没有结果的变量。
您可以尝试使用 this 将使用变量 $comments 的语句括起来。
对于控制器或其他文件 php
if (!$comment->isEmpty()) {
//your code
}
if ($comment->count()) {
//your code
}
if (count($comment)) {
//your code
}
刀片
@if(!$comment->isEmpty())
//your code
@endif
@if($comment->count())
//your code
@endif
@if(count($comment))
//your code
@endif
我希望我能帮助你,如果没有,请附上更多的代码,它们与他所说的完全一样,因为评论并删除图片,因为我没有在你所附的代码中看到。谢谢,祝你好运。
更新
<div class="row">
<div class="col-md-12">
@if(!$post->comments->isEmpty()) //****Added
@if($post->comments->count() > 0)
@foreach($post->comments as $comment)
<div class="comment d-flex ">
<p><strong><a class="text-dark"
href="/profile/{{ $comment->user_id }}">{{ $comment->username}}</a>:
</strong> {{ isset($comment->comment) ? $comment->comment : "--" }}</p>
@can('update', $post->user->profile)
<div class="dropdown col-md-6">
<button type="button" class="btn btn-primary dropdown-toggle btn-sm"
style="background-color: #ffffff00;border: 1px solid #555;color: black;padding: 0 5px"
data-toggle="dropdown">
Select
</button>
<div class="dropdown-menu">
<a class="dropdown-item" href="#">Edit comment</a>
<a class="dropdown-item" title="Options" style="text-decoration: none;"
href="/deleteComment/{{$comment->id}}">Delete comment</a>
</div>
</div>
</div>
@endcan
@endforeach
@endif
@endif //****Added
</div>
</div>
更新删除如果管理员或没有
/**
* Comments Controller Method Delete
*/
public function delete($id)
{
$comment = DB::table('comments')->where('id', $id):
if(!auth()->user()->admin){
$comment->where('user_id', auth()->user()->id);
}
if (!$comment->delete()) {
Session::flash('remove', "You do not have permission to delete the comment!");
} else {
Session::flash('remove', "Message removed successfully!");
}
return Redirect::back();
}
TA贡献1813条经验 获得超2个赞
第一个问题可以很容易地完成。在您的destroy()函数中,只需检查评论所有者:
// Check comment owner
if($comment->user_id != \Auth::id()){
return abort(401);
}
// Do logic code to delete comment.
第二个问题,您可以像这样检查存在评论:
if(! $comments->isEmpty()) {
// Do logic code to show comment
}
- 2 回答
- 0 关注
- 223 浏览
添加回答
举报