feat: add many playlist for music
This commit is contained in:
@@ -20,12 +20,11 @@ class Music extends Model
|
||||
|
||||
|
||||
protected $fillable = [
|
||||
'user_id', 'title', 'artist', 'file_path', 'type',
|
||||
'playlist_id', 'image_id', 'duration', 'order', 'is_active'
|
||||
'user_id', 'title', 'artist', 'file_path', 'type',
|
||||
'image_id', 'duration', 'is_active'
|
||||
];
|
||||
protected $casts = [
|
||||
'duration' => 'integer',
|
||||
'order' => 'integer',
|
||||
'is_active' => 'boolean',
|
||||
];
|
||||
protected $attributes = [
|
||||
@@ -36,9 +35,11 @@ public function user()
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
public function playlist(): BelongsTo
|
||||
public function playlists(): BelongsToMany
|
||||
{
|
||||
return $this->belongsTo(MusicPlaylist::class, 'playlist_id');
|
||||
return $this->belongsToMany(MusicPlaylist::class, 'music_playlist', 'music_id', 'playlist_id')
|
||||
->withPivot('order')
|
||||
->withTimestamps();
|
||||
}
|
||||
public function tags(): BelongsToMany
|
||||
{
|
||||
|
||||
@@ -43,9 +43,11 @@ public function subcategories(): BelongsToMany
|
||||
}
|
||||
|
||||
|
||||
public function musics(): HasMany
|
||||
public function musics(): BelongsToMany
|
||||
{
|
||||
return $this->hasMany(Music::class, 'playlist_id');
|
||||
return $this->belongsToMany(Music::class, 'music_playlist', 'playlist_id', 'music_id')
|
||||
->withPivot('order')
|
||||
->withTimestamps();
|
||||
}
|
||||
|
||||
public function image(): BelongsTo
|
||||
@@ -55,11 +57,11 @@ public function image(): BelongsTo
|
||||
|
||||
public function getActiveMusicsAttribute()
|
||||
{
|
||||
return $this->musics()->where('is_active', true)->orderBy('order')->get();
|
||||
return $this->musics()->where('music.is_active', true)->orderBy('music_playlist.order')->get();
|
||||
}
|
||||
|
||||
|
||||
public function getTotalDurationAttribute()
|
||||
{
|
||||
return $this->musics()->where('is_active', true)->sum('duration');
|
||||
return $this->musics()->where('music.is_active', true)->sum('music.duration');
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user