在 Laravel 项目中为评论创建 Like 系统时遇到问题
Having trouble creating a Like system for comments in a Laravel Project
我正在使用 Laravel 创建一个项目。用户可以点赞评论。我想显示一个 "like" 按钮,以便用户可以喜欢评论,如果用户已经喜欢该评论,我希望该按钮是 "unlike" 这样用户就可以对喜欢的评论不喜欢
在我的数据库中我有一个赞table:
| id | user_id | comment_id |
我喜欢的模特是这样的:
class Like extends \Eloquent {
protected $fillable = ['user_id', 'comment_id'];
protected $table = 'likes';
public function owner()
{
return $this->belongsTo('Acme\Users\User', 'user_id');
}
}
评论模型如下所示:
class Comment extends \Eloquent {
protected $fillable = ['user_id', 'post_id', 'body'];
protected $table = 'comments';
public function owner()
{
return $this->belongsTo('Acme\Users\User', 'user_id');
}
public function likes()
{
return $this->hasMany('Acme\Likes\Like');
}
}
用户模型:
class User extends Eloquent {
public function comments()
{
return $this->hasMany('Acme\Comments\Comment');
}
public function likes()
{
return $this->hasMany('Acme\Likes\Like');
}
}
喜欢控制器:
class LikesController extends \BaseController {
use CommanderTrait;
/**
* Like a comment
* @return Response
*/
public function commentLike()
{
// using a command bus. Basically making a post to the likes table assigning user_id and comment_id then redirect back
extract(Input::only('user_id', 'comment_id'));
$this->execute(new CommentLikeCommand($user_id, $comment_id));
return Redirect::back();
}
public function unlike()
{
$like = new Like;
$user = Auth::user();
$id = Input::only('comment_id');
$like->where('user_id', $user->id)->where('comment_id', $id)->first()->delete();
return Redirect::back();
}
}
在我看来,我可以通过 $comment 获取评论,并且可以通过 $comment->like have:
@foreach($post->comments as $comment)
<div class="user-comment">
<p class="comment">
{{ $comment->owner->first_name }} {{ $comment->owner->last_name }} {{ $comment->body }}
</p>
<div class="com-details">
<!-- how long ago the comment was posted -->
<div class="com-time-container">
{{ $comment->created_at->diffForHumans() }} ·
</div>
<!-- HERE IS WHERE I WANT THE LIKE AND UNLIKE BUTTONS TO DISPLAY -->
@if ($comment->likes->owner->id === $currentUser->id)
{{ Form::open(['route' => 'like']) }}
{{ Form::hidden('user_id', $currentUser->id) }}
{{ Form::hidden('comment_id', $comment->id) }}
<button type="submit" class="com-like">Like</button>
{{ Form::close() }}
@else
{{ Form::open(['route' => 'unlike']) }}
{{ Form::hidden('user_id', $currentUser->id) }}
{{ Form::hidden('comment_id', $comment->id) }}
<button type="submit" class="com-like">Unlike</button>
{{ Form::close() }}
@endif
<!-- how many users like this comment -->
<span class="likes"> · {{ $comment->likes->count() }}</span>
</div>
</div><!--user-comment end-->
@endforeach
我正在尝试设置一个 if 语句来查看当前用户是否喜欢该状态,但我不确定这是如何完成的?如果用户还没有喜欢评论,我希望显示 "like" 按钮。如果用户喜欢我希望显示 "unlike" 按钮的评论。我以为我可以说 @if($comment->likes->owner->id === $currentUser->id)
但我得到未定义 属性。我该怎么做?
我用 Laravel 的工作不多,所以我的语法和术语可能有点偏差,但它应该是正确的想法。
基本上,$comment->likes
是该评论的点赞列表。您需要遍历这些点赞并检查其中一个点赞是否来自当前用户。如果其中之一是当前用户,则显示不同按钮。否则,显示赞按钮。
不确定在 Blade 中会是什么样子,但这里有一些伪代码:
$currentUserLiked = false;
// go through all of the comment's likes
foreach ($comment->likes as $like)
{
// check if this like is by the current user
if ($like->owner->id == $currentUser->id)
{
$currentUserLiked = true;
break;
}
}
if ($currentUserLiked)
{
showUnlikeButton();
}
else
{
showLikeButton();
}
$comment->likes
是 Like
个对象的集合。要访问 owner
属性,您需要迭代集合。
但是,另一种选择是使用集合中的可用方法来执行您需要的操作:
@if (in_array($currentUser->id, $comment->likes->lists('user_id')))
$comment->likes->lists('user_id')
将 return 一个数组,其中包含评论的点赞集合中的所有 user_id
值。 in_array()
将检查 $currentUser->id
是否在该数组中。
我正在使用 Laravel 创建一个项目。用户可以点赞评论。我想显示一个 "like" 按钮,以便用户可以喜欢评论,如果用户已经喜欢该评论,我希望该按钮是 "unlike" 这样用户就可以对喜欢的评论不喜欢
在我的数据库中我有一个赞table:
| id | user_id | comment_id |
我喜欢的模特是这样的:
class Like extends \Eloquent {
protected $fillable = ['user_id', 'comment_id'];
protected $table = 'likes';
public function owner()
{
return $this->belongsTo('Acme\Users\User', 'user_id');
}
}
评论模型如下所示:
class Comment extends \Eloquent {
protected $fillable = ['user_id', 'post_id', 'body'];
protected $table = 'comments';
public function owner()
{
return $this->belongsTo('Acme\Users\User', 'user_id');
}
public function likes()
{
return $this->hasMany('Acme\Likes\Like');
}
}
用户模型:
class User extends Eloquent {
public function comments()
{
return $this->hasMany('Acme\Comments\Comment');
}
public function likes()
{
return $this->hasMany('Acme\Likes\Like');
}
}
喜欢控制器:
class LikesController extends \BaseController {
use CommanderTrait;
/**
* Like a comment
* @return Response
*/
public function commentLike()
{
// using a command bus. Basically making a post to the likes table assigning user_id and comment_id then redirect back
extract(Input::only('user_id', 'comment_id'));
$this->execute(new CommentLikeCommand($user_id, $comment_id));
return Redirect::back();
}
public function unlike()
{
$like = new Like;
$user = Auth::user();
$id = Input::only('comment_id');
$like->where('user_id', $user->id)->where('comment_id', $id)->first()->delete();
return Redirect::back();
}
}
在我看来,我可以通过 $comment 获取评论,并且可以通过 $comment->like have:
@foreach($post->comments as $comment)
<div class="user-comment">
<p class="comment">
{{ $comment->owner->first_name }} {{ $comment->owner->last_name }} {{ $comment->body }}
</p>
<div class="com-details">
<!-- how long ago the comment was posted -->
<div class="com-time-container">
{{ $comment->created_at->diffForHumans() }} ·
</div>
<!-- HERE IS WHERE I WANT THE LIKE AND UNLIKE BUTTONS TO DISPLAY -->
@if ($comment->likes->owner->id === $currentUser->id)
{{ Form::open(['route' => 'like']) }}
{{ Form::hidden('user_id', $currentUser->id) }}
{{ Form::hidden('comment_id', $comment->id) }}
<button type="submit" class="com-like">Like</button>
{{ Form::close() }}
@else
{{ Form::open(['route' => 'unlike']) }}
{{ Form::hidden('user_id', $currentUser->id) }}
{{ Form::hidden('comment_id', $comment->id) }}
<button type="submit" class="com-like">Unlike</button>
{{ Form::close() }}
@endif
<!-- how many users like this comment -->
<span class="likes"> · {{ $comment->likes->count() }}</span>
</div>
</div><!--user-comment end-->
@endforeach
我正在尝试设置一个 if 语句来查看当前用户是否喜欢该状态,但我不确定这是如何完成的?如果用户还没有喜欢评论,我希望显示 "like" 按钮。如果用户喜欢我希望显示 "unlike" 按钮的评论。我以为我可以说 @if($comment->likes->owner->id === $currentUser->id)
但我得到未定义 属性。我该怎么做?
我用 Laravel 的工作不多,所以我的语法和术语可能有点偏差,但它应该是正确的想法。
基本上,$comment->likes
是该评论的点赞列表。您需要遍历这些点赞并检查其中一个点赞是否来自当前用户。如果其中之一是当前用户,则显示不同按钮。否则,显示赞按钮。
不确定在 Blade 中会是什么样子,但这里有一些伪代码:
$currentUserLiked = false;
// go through all of the comment's likes
foreach ($comment->likes as $like)
{
// check if this like is by the current user
if ($like->owner->id == $currentUser->id)
{
$currentUserLiked = true;
break;
}
}
if ($currentUserLiked)
{
showUnlikeButton();
}
else
{
showLikeButton();
}
$comment->likes
是 Like
个对象的集合。要访问 owner
属性,您需要迭代集合。
但是,另一种选择是使用集合中的可用方法来执行您需要的操作:
@if (in_array($currentUser->id, $comment->likes->lists('user_id')))
$comment->likes->lists('user_id')
将 return 一个数组,其中包含评论的点赞集合中的所有 user_id
值。 in_array()
将检查 $currentUser->id
是否在该数组中。