engagement system
This commit is contained in:
@@ -197,4 +197,68 @@ class Product extends Model
|
||||
'approved_at' => null,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the upvotes for the product.
|
||||
*/
|
||||
public function upvotes()
|
||||
{
|
||||
return $this->hasMany(Upvote::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the comments for the product.
|
||||
*/
|
||||
public function comments()
|
||||
{
|
||||
return $this->hasMany(Comment::class)->latest();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the bookmarks for the product.
|
||||
*/
|
||||
public function bookmarks()
|
||||
{
|
||||
return $this->hasMany(Bookmark::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a user has upvoted the product.
|
||||
*/
|
||||
public function isUpvotedBy(?User $user): bool
|
||||
{
|
||||
if (!$user) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $this->upvotes()->where('user_id', $user->id)->exists();
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a user has bookmarked the product.
|
||||
*/
|
||||
public function isBookmarkedBy(?User $user): bool
|
||||
{
|
||||
if (!$user) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $this->bookmarks()->where('user_id', $user->id)->exists();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the upvotes count for the product.
|
||||
*/
|
||||
public function getUpvotesCountAttribute(): int
|
||||
{
|
||||
return $this->upvotes()->count();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the comments count for the product.
|
||||
*/
|
||||
public function getCommentsCountAttribute(): int
|
||||
{
|
||||
return $this->comments()->count();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user