feat: add save feature
This commit is contained in:
@@ -3,16 +3,18 @@
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use App\Traits\HasSaves;
|
||||
|
||||
class BreathingTemplate extends Model
|
||||
{
|
||||
use HasSaves;
|
||||
protected $fillable = ['user_id', 'name', 'inhale', 'exhale', 'breath_hold' , 'duration' , 'description','image_id' , 'source'];
|
||||
|
||||
public function user()
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
protected $appends = ['image_url'];
|
||||
protected $appends = ['image_url', 'is_saved','saved_count'];
|
||||
|
||||
public function getImageUrlAttribute()
|
||||
{
|
||||
|
||||
+12
-8
@@ -5,9 +5,11 @@
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use App\Traits\HasRatings;
|
||||
use App\Traits\HasComments;
|
||||
use App\Traits\HasSaves;
|
||||
|
||||
class Media extends Model
|
||||
{
|
||||
use HasRatings, HasComments;
|
||||
use HasRatings, HasComments,HasSaves;
|
||||
protected $fillable = [
|
||||
'user_id',
|
||||
'image_id',
|
||||
@@ -21,16 +23,18 @@ class Media extends Model
|
||||
'visibility',
|
||||
'is_premium'
|
||||
];
|
||||
protected $appends = [
|
||||
'average_rating',
|
||||
protected $appends = [
|
||||
'average_rating',
|
||||
'user_rating',
|
||||
'ratings_count',
|
||||
'comments_count',
|
||||
'has_user_commented', // Add this
|
||||
'user_comment', // Add this
|
||||
'user_comment_id', // Add this
|
||||
'has_user_rated' // Add this
|
||||
];
|
||||
'has_user_commented',
|
||||
'user_comment',
|
||||
'user_comment_id',
|
||||
'has_user_rated',
|
||||
'is_saved',
|
||||
'saved_count'
|
||||
];
|
||||
public function image()
|
||||
{
|
||||
return $this->belongsTo(Image::class);
|
||||
|
||||
+11
-3
@@ -10,10 +10,11 @@
|
||||
use Illuminate\Database\Eloquent\Relations\MorphMany; // ← Add this
|
||||
use App\Traits\HasRatings;
|
||||
use App\Traits\HasComments;
|
||||
use App\Traits\HasSaves;
|
||||
|
||||
class Music extends Model
|
||||
{
|
||||
use HasFactory, HasRatings, HasComments;
|
||||
use HasFactory, HasRatings, HasComments , HasSaves;
|
||||
protected $table = 'music';
|
||||
|
||||
|
||||
@@ -43,10 +44,17 @@ public function tags(): BelongsToMany
|
||||
return $this->belongsToMany(Tag::class, 'music_tags');
|
||||
}
|
||||
|
||||
protected $appends = ['url', 'image_url' , 'average_rating', 'user_rating', 'comments_count' , 'has_user_commented',
|
||||
protected $appends = ['url',
|
||||
'image_url' ,
|
||||
'average_rating',
|
||||
'user_rating',
|
||||
'comments_count' ,
|
||||
'has_user_commented',
|
||||
'user_comment',
|
||||
'user_comment_id',
|
||||
'has_user_rated'
|
||||
'has_user_rated' ,
|
||||
'is_saved',
|
||||
'saved_count'
|
||||
];
|
||||
|
||||
public function getUrlAttribute()
|
||||
|
||||
@@ -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