feat: add like table
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\MorphTo;
|
||||
class Like extends Model
|
||||
{
|
||||
protected $table = 'likes';
|
||||
|
||||
protected $fillable = [
|
||||
'user_id',
|
||||
'likeable_id',
|
||||
'likeable_type',
|
||||
];
|
||||
|
||||
public function user(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
|
||||
public function likeable(): MorphTo
|
||||
{
|
||||
return $this->morphTo();
|
||||
}
|
||||
|
||||
// Helper methods
|
||||
public static function getLikedItemsForUser($userId, $type = null)
|
||||
{
|
||||
$query = self::with('likeable')->where('user_id', $userId);
|
||||
|
||||
if ($type) {
|
||||
$query->where('likeable_type', $type);
|
||||
}
|
||||
|
||||
return $query->latest()->get();
|
||||
}
|
||||
|
||||
public static function isLikedByUser($userId, $likeableId, $likeableType)
|
||||
{
|
||||
return self::where([
|
||||
'user_id' => $userId,
|
||||
'likeable_id' => $likeableId,
|
||||
'likeable_type' => $likeableType,
|
||||
])->exists();
|
||||
}
|
||||
|
||||
public static function getLikeCount($likeableId, $likeableType)
|
||||
{
|
||||
return self::where([
|
||||
'likeable_id' => $likeableId,
|
||||
'likeable_type' => $likeableType,
|
||||
])->count();
|
||||
}
|
||||
}
|
||||
@@ -33,7 +33,9 @@ class Media extends Model
|
||||
'user_comment_id',
|
||||
'has_user_rated',
|
||||
'is_saved',
|
||||
'saved_count'
|
||||
'saved_count',
|
||||
'is_liked',
|
||||
'likes_count'
|
||||
];
|
||||
public function image()
|
||||
{
|
||||
|
||||
@@ -11,10 +11,11 @@
|
||||
use App\Traits\HasRatings;
|
||||
use App\Traits\HasComments;
|
||||
use App\Traits\HasSaves;
|
||||
use App\Traits\HasLikes;
|
||||
|
||||
class Music extends Model
|
||||
{
|
||||
use HasFactory, HasRatings, HasComments , HasSaves;
|
||||
use HasFactory, HasRatings, HasComments , HasSaves,HasLikes;
|
||||
protected $table = 'music';
|
||||
|
||||
|
||||
@@ -54,7 +55,9 @@ public function tags(): BelongsToMany
|
||||
'user_comment_id',
|
||||
'has_user_rated' ,
|
||||
'is_saved',
|
||||
'saved_count'
|
||||
'saved_count',
|
||||
'is_liked',
|
||||
'likes_count'
|
||||
];
|
||||
|
||||
public function getUrlAttribute()
|
||||
|
||||
Reference in New Issue
Block a user