engagement system

This commit is contained in:
2026-02-06 15:13:20 +02:00
parent 2e1ce0d93b
commit 7a99c3eb59
15 changed files with 1079 additions and 2 deletions
+64
View File
@@ -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();
}
}