Files
2026-05-24 23:21:00 +03:30

57 lines
1.3 KiB
PHP

<?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();
}
}