当用户点击赞按钮时,只显示赞按钮
When user click on like butten then show only like button
// LIke the page code
public function like($id){
$user_id = Auth::user()->id;
$like_user = article_likes::where(
['user_id'=>$user_id,
'article_id' => $id]
)->first();
if(empty($like_user->user_id)){
$user_id = Auth::user()->id;
$article_id = $id;
$likes = new article_likes();
$likes->user_id = $user_id;
$likes->article_id = $article_id;
$likes->save();
return redirect()->back();
}else{
return redirect()->back();
}
}
// Dislike the page
public function dislike($id){
$user_id = Auth::user()->id;
$like_user = article_dislikes::where(
['user_id'=>$user_id,
'article_id' => $id]
)->first();
if(empty($like_user->user_id)){
$user_id = Auth::user()->id;
$article_id = $id;
$likes = new article_dislikes();
$likes->user_id = $user_id;
$likes->article_id = $article_id;
$likes->save();
return redirect()->back();
}else{
return redirect()->back();
}
}
Blade 页数
<div>
<span class="">You think it is helpfull?</span>
<a href="{{url('/likes/'.$articles->id)}}" class="btn btn-success btn-sm"><i class="fa fa-thumbs-up"></i></a>
<a href="{{url('/dislikes/'.$articles->id)}}" class="btn btn-danger btn-sm"><i class="fa fa-thumbs-down"></i></a>
</div>
我想实现这样的场景:当用户通过点击赞按钮喜欢post时,只显示赞按钮,不显示不喜欢按钮。同样,当用户点击不喜欢按钮时,显示不喜欢按钮,而不是喜欢按钮。
我不知道你的控制器长什么样,所以我会这样做:
class ArticleController extends Controller
{
public function show(Article $article)
{
$dislike_user = article_dislikes::where(
['user_id'=>$user_id,
'article_id' => $id]
)->first();
$like_user = article_likes::where(
['user_id'=>$user_id,
'article_id' => $id]
)->first();
$rating = 0;
if ($like_user !== null) {
$rating = 1;
}
if ($dislike_user !== null) {
$rating = -1;
}
return view('article', compact('article', 'rating'));
}
}
<div>
<span class="">You think it is helpfull?</span>
@if($rating === 1)
<a href="{{url('/likes/'.$articles->id)}}" class="btn btn-success btn-sm"><i class="fa fa-thumbs-up"></i></a>
@endif
@if($rating === -1)
<a href="{{url('/dislikes/'.$articles->id)}}" class="btn btn-danger btn-sm"><i class="fa fa-thumbs-down"></i></a>
@endif
</div>
// LIke the page code
public function like($id){
$user_id = Auth::user()->id;
$like_user = article_likes::where(
['user_id'=>$user_id,
'article_id' => $id]
)->first();
if(empty($like_user->user_id)){
$user_id = Auth::user()->id;
$article_id = $id;
$likes = new article_likes();
$likes->user_id = $user_id;
$likes->article_id = $article_id;
$likes->save();
return redirect()->back();
}else{
return redirect()->back();
}
}
// Dislike the page
public function dislike($id){
$user_id = Auth::user()->id;
$like_user = article_dislikes::where(
['user_id'=>$user_id,
'article_id' => $id]
)->first();
if(empty($like_user->user_id)){
$user_id = Auth::user()->id;
$article_id = $id;
$likes = new article_dislikes();
$likes->user_id = $user_id;
$likes->article_id = $article_id;
$likes->save();
return redirect()->back();
}else{
return redirect()->back();
}
}
Blade 页数
<div>
<span class="">You think it is helpfull?</span>
<a href="{{url('/likes/'.$articles->id)}}" class="btn btn-success btn-sm"><i class="fa fa-thumbs-up"></i></a>
<a href="{{url('/dislikes/'.$articles->id)}}" class="btn btn-danger btn-sm"><i class="fa fa-thumbs-down"></i></a>
</div>
我想实现这样的场景:当用户通过点击赞按钮喜欢post时,只显示赞按钮,不显示不喜欢按钮。同样,当用户点击不喜欢按钮时,显示不喜欢按钮,而不是喜欢按钮。
我不知道你的控制器长什么样,所以我会这样做:
class ArticleController extends Controller
{
public function show(Article $article)
{
$dislike_user = article_dislikes::where(
['user_id'=>$user_id,
'article_id' => $id]
)->first();
$like_user = article_likes::where(
['user_id'=>$user_id,
'article_id' => $id]
)->first();
$rating = 0;
if ($like_user !== null) {
$rating = 1;
}
if ($dislike_user !== null) {
$rating = -1;
}
return view('article', compact('article', 'rating'));
}
}
<div>
<span class="">You think it is helpfull?</span>
@if($rating === 1)
<a href="{{url('/likes/'.$articles->id)}}" class="btn btn-success btn-sm"><i class="fa fa-thumbs-up"></i></a>
@endif
@if($rating === -1)
<a href="{{url('/dislikes/'.$articles->id)}}" class="btn btn-danger btn-sm"><i class="fa fa-thumbs-down"></i></a>
@endif
</div>