feat: add like table

This commit is contained in:
2026-05-24 23:21:00 +03:30
parent d8f769e738
commit 5800f64f88
7 changed files with 399 additions and 3 deletions
+56
View File
@@ -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();
}
}