feat: add save feature
This commit is contained in:
@@ -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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user