feat: add save feature

This commit is contained in:
2026-05-23 01:09:29 +03:30
parent e314f95656
commit 8339a03168
8 changed files with 401 additions and 13 deletions
+49
View File
@@ -0,0 +1,49 @@
<?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();
}
}