feat: add sub category
This commit is contained in:
@@ -22,7 +22,22 @@ public function playlists(): HasMany
|
||||
{
|
||||
return $this->hasMany(MusicPlaylist::class, 'category_id');
|
||||
}
|
||||
|
||||
|
||||
public function subcategories(): HasMany
|
||||
{
|
||||
return $this->hasMany(MusicSubcategory::class, 'category_id');
|
||||
}
|
||||
// All playlists (including those in subcategories)
|
||||
public function allPlaylists()
|
||||
{
|
||||
$playlists = collect($this->playlists);
|
||||
|
||||
foreach ($this->subcategories as $subcategory) {
|
||||
$playlists = $playlists->merge($subcategory->playlists);
|
||||
}
|
||||
|
||||
return $playlists;
|
||||
}
|
||||
public function image(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Image::class);
|
||||
@@ -32,4 +47,18 @@ public function getActivePlaylistsAttribute()
|
||||
{
|
||||
return $this->playlists()->where('is_active', true)->get();
|
||||
}
|
||||
|
||||
// Total music count across all playlists and subcategories
|
||||
public function getTotalMusicCountAttribute()
|
||||
{
|
||||
$count = $this->playlists->sum(function($playlist) {
|
||||
return $playlist->musics->count();
|
||||
});
|
||||
|
||||
foreach ($this->subcategories as $subcategory) {
|
||||
$count += $subcategory->total_music_count;
|
||||
}
|
||||
|
||||
return $count;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@ class MusicPlaylist extends Model
|
||||
protected $table = 'music_playlists';
|
||||
|
||||
protected $fillable = [
|
||||
'category_id', 'name', 'slug', 'description', 'image_id', 'order', 'is_active'
|
||||
'category_id', 'subcategory_id','name', 'slug', 'description', 'image_id', 'order', 'is_active'
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
@@ -23,6 +23,12 @@ public function category(): BelongsTo
|
||||
return $this->belongsTo(MusicCategory::class, 'category_id');
|
||||
}
|
||||
|
||||
public function subcategory(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(MusicSubcategory::class, 'subcategory_id');
|
||||
}
|
||||
|
||||
|
||||
public function musics(): HasMany
|
||||
{
|
||||
return $this->hasMany(Music::class, 'playlist_id');
|
||||
|
||||
@@ -0,0 +1,85 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
class MusicSubcategory extends Model
|
||||
{
|
||||
protected $table = 'music_subcategories';
|
||||
|
||||
protected $fillable = [
|
||||
'category_id', 'name', 'slug', 'description', 'image_id', 'order', 'is_active'
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'is_active' => 'boolean',
|
||||
'order' => 'integer',
|
||||
];
|
||||
|
||||
public function category(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(MusicCategory::class, 'category_id');
|
||||
}
|
||||
|
||||
public function playlists(): HasMany
|
||||
{
|
||||
return $this->hasMany(MusicPlaylist::class, 'subcategory_id');
|
||||
}
|
||||
|
||||
public function image(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Image::class);
|
||||
}
|
||||
|
||||
public function getActivePlaylistsAttribute()
|
||||
{
|
||||
return $this->playlists()->where('is_active', true)->orderBy('order')->get();
|
||||
}
|
||||
|
||||
// Get total music count across all playlists in this subcategory
|
||||
public function getTotalMusicCountAttribute()
|
||||
{
|
||||
return $this->playlists()
|
||||
->withCount('musics')
|
||||
->get()
|
||||
->sum('musics_count');
|
||||
}
|
||||
|
||||
// Get total duration across all music in this subcategory
|
||||
public function getTotalDurationAttribute()
|
||||
{
|
||||
$totalSeconds = 0;
|
||||
foreach ($this->playlists as $playlist) {
|
||||
foreach ($playlist->musics as $music) {
|
||||
$totalSeconds += $this->durationToSeconds($music->duration);
|
||||
}
|
||||
}
|
||||
return $this->secondsToDuration($totalSeconds);
|
||||
}
|
||||
|
||||
private function durationToSeconds($duration)
|
||||
{
|
||||
if (!$duration) return 0;
|
||||
$parts = explode(':', $duration);
|
||||
if (count($parts) === 2) {
|
||||
return (int)$parts[0] * 60 + (int)$parts[1];
|
||||
} elseif (count($parts) === 3) {
|
||||
return (int)$parts[0] * 3600 + (int)$parts[1] * 60 + (int)$parts[2];
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
private function secondsToDuration($seconds)
|
||||
{
|
||||
$hours = floor($seconds / 3600);
|
||||
$minutes = floor(($seconds % 3600) / 60);
|
||||
$secs = $seconds % 60;
|
||||
|
||||
if ($hours > 0) {
|
||||
return sprintf("%d:%02d:%02d", $hours, $minutes, $secs);
|
||||
}
|
||||
return sprintf("%d:%02d", $minutes, $secs);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user