83 lines
2.4 KiB
PHP
83 lines
2.4 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo; // ← Add this
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany; // ← Add this
|
|
use App\Traits\HasComments; // Add this
|
|
use App\Traits\HasLikes;
|
|
use App\Traits\HasSaves;
|
|
use App\Traits\HasRatings;
|
|
class MusicPlaylist extends Model
|
|
{
|
|
use HasComments, HasLikes, HasSaves, HasRatings;
|
|
protected $table = 'music_playlists';
|
|
|
|
protected $fillable = [
|
|
'name', 'slug', 'description', 'image_id', 'detail_image_id', 'order', 'is_active', 'is_premium'
|
|
];
|
|
|
|
protected $casts = [
|
|
'is_active' => 'boolean',
|
|
'is_premium' => 'boolean',
|
|
'order' => 'integer',
|
|
];
|
|
protected $appends = [
|
|
'comments_count',
|
|
'has_user_commented',
|
|
'user_comment',
|
|
'user_comment_id',
|
|
'is_liked',
|
|
'likes_count',
|
|
'is_saved',
|
|
'saved_count',
|
|
'duration',
|
|
];
|
|
public function categories(): BelongsToMany
|
|
{
|
|
return $this->belongsToMany(MusicCategory::class, 'music_category_playlist', 'playlist_id', 'category_id');
|
|
}
|
|
|
|
public function subcategories(): BelongsToMany
|
|
{
|
|
return $this->belongsToMany(MusicSubcategory::class, 'music_subcategory_playlist', 'playlist_id', 'subcategory_id');
|
|
}
|
|
|
|
|
|
public function musics(): BelongsToMany
|
|
{
|
|
return $this->belongsToMany(Music::class, 'music_playlist', 'playlist_id', 'music_id')
|
|
->withPivot('order')
|
|
->withTimestamps();
|
|
}
|
|
|
|
public function image(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Image::class);
|
|
}
|
|
|
|
// Image shown on the detail (show-by-id) screen; image() is the list thumbnail.
|
|
public function detailImage(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Image::class, 'detail_image_id');
|
|
}
|
|
|
|
public function getActiveMusicsAttribute()
|
|
{
|
|
return $this->musics()->where('music.is_active', true)->orderBy('music_playlist.order')->get();
|
|
}
|
|
|
|
public function getTotalDurationAttribute()
|
|
{
|
|
return $this->musics()->where('music.is_active', true)->sum('music.duration');
|
|
}
|
|
|
|
// Total play time of the playlist = sum of its active musics' durations.
|
|
public function getDurationAttribute()
|
|
{
|
|
return $this->total_duration;
|
|
}
|
|
}
|