engagement system
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
class Bookmark extends Model
|
||||
{
|
||||
/**
|
||||
* Indicates if the model should be timestamped.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
public $timestamps = false;
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*
|
||||
* @var array<int, string>
|
||||
*/
|
||||
protected $fillable = [
|
||||
'user_id',
|
||||
'product_id',
|
||||
];
|
||||
|
||||
/**
|
||||
* Get the user that owns the bookmark.
|
||||
*/
|
||||
public function user(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the product that owns the bookmark.
|
||||
*/
|
||||
public function product(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Product::class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
class Comment extends Model
|
||||
{
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*
|
||||
* @var array<int, string>
|
||||
*/
|
||||
protected $fillable = [
|
||||
'user_id',
|
||||
'product_id',
|
||||
'body',
|
||||
];
|
||||
|
||||
/**
|
||||
* Get the user that owns the comment.
|
||||
*/
|
||||
public function user(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the product that owns the comment.
|
||||
*/
|
||||
public function product(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Product::class);
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
class Upvote extends Model
|
||||
{
|
||||
/**
|
||||
* Indicates if the model should be timestamped.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
public $timestamps = false;
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*
|
||||
* @var array<int, string>
|
||||
*/
|
||||
protected $fillable = [
|
||||
'user_id',
|
||||
'product_id',
|
||||
];
|
||||
|
||||
/**
|
||||
* Get the user that owns the upvote.
|
||||
*/
|
||||
public function user(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the product that owns the upvote.
|
||||
*/
|
||||
public function product(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Product::class);
|
||||
}
|
||||
}
|
||||
@@ -120,4 +120,28 @@ class User extends Authenticatable
|
||||
{
|
||||
return $this->hasMany(Product::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the upvotes for the user.
|
||||
*/
|
||||
public function upvotes(): HasMany
|
||||
{
|
||||
return $this->hasMany(Upvote::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the comments for the user.
|
||||
*/
|
||||
public function comments(): HasMany
|
||||
{
|
||||
return $this->hasMany(Comment::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the bookmarks for the user.
|
||||
*/
|
||||
public function bookmarks(): HasMany
|
||||
{
|
||||
return $this->hasMany(Bookmark::class);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user