我有2个问题,我需要你所有的help.First,我已经做了评论删除路线,但其他用户登录可以从直接的联系也删除评论... link.com/deleteComment/id。如何使此仅对评论的所有者可用?所有者 ID 保存在数据库中,可以使用{{ $comment->user_id }}.
第二个问题...在我看来,当我点击一张没有评论的照片时,我收到了undefined variable comment,但我不知道为什么,因为在有评论的照片上,我没有问题。我可以做点什么if comments != empty, dont show it吗?像那样?
评论控制器:
public function store(Request $request, $post_id)
{
$this->validate($request, array(
'comment' => 'required|min:5|max:2000',
));
$post = Post::find($post_id);
$comment = new Comment();
$comment->username = Auth::user()->username;
$comment->email = Auth::user()->email;
$comment->user_id = Auth::user()->id;
$comment->comment = $request->comment;
$comment->approved = true;
$comment->post()->associate($post);
$comment->save();
Session::flash('message', "Message posted successfully!");
return Redirect::back();
}
帖子控制器:
public function delete($id){
DB::table('posts')->where('id',$id)->delete();
return redirect('/profile/' . auth()->user()->id);
}
我的看法
@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> {{ $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
@endforeac
慕工程0101907
慕姐8265434