Files
2026-05-23 01:09:29 +03:30

50 lines
1.2 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\MorphTo;
class SavedItem extends Model
{
protected $table = 'saved_items';
protected $fillable = [
'user_id',
'saveable_id',
'saveable_type',
];
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
public function saveable(): MorphTo
{
return $this->morphTo();
}
// Helper to get saved items by type
public static function getSavedItemsForUser($userId, $type = null)
{
$query = self::with('saveable')->where('user_id', $userId);
if ($type) {
$query->where('saveable_type', $type);
}
return $query->latest()->get();
}
// Check if user has saved specific item
public static function isSavedByUser($userId, $saveableId, $saveableType)
{
return self::where([
'user_id' => $userId,
'saveable_id' => $saveableId,
'saveable_type' => $saveableType,
])->exists();
}
}