feat: many category and sub can be in playlist
This commit is contained in:
@@ -8,16 +8,23 @@
|
|||||||
|
|
||||||
class MusicPlaylistController extends Controller
|
class MusicPlaylistController extends Controller
|
||||||
{
|
{
|
||||||
public function index(Request $request)
|
public function index(Request $request, $categoryId = null)
|
||||||
{
|
{
|
||||||
$query = MusicPlaylist::with(['category', 'subcategory', 'image']);
|
$query = MusicPlaylist::with(['categories', 'subcategories', 'image']);
|
||||||
|
|
||||||
if ($request->has('category_id')) {
|
$categoryId = $categoryId ?? $request->input('category_id');
|
||||||
$query->where('category_id', $request->category_id)->whereNull('subcategory_id');
|
|
||||||
|
if ($categoryId) {
|
||||||
|
$query->whereHas('categories', function ($q) use ($categoryId) {
|
||||||
|
$q->where('music_categories.id', $categoryId);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($request->has('subcategory_id')) {
|
if ($request->filled('subcategory_id')) {
|
||||||
$query->where('subcategory_id', $request->subcategory_id);
|
$subcategoryId = $request->input('subcategory_id');
|
||||||
|
$query->whereHas('subcategories', function ($q) use ($subcategoryId) {
|
||||||
|
$q->where('music_subcategories.id', $subcategoryId);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
$playlists = $query->where('is_active', true)->orderBy('order')->get();
|
$playlists = $query->where('is_active', true)->orderBy('order')->get();
|
||||||
@@ -28,8 +35,10 @@ public function index(Request $request)
|
|||||||
public function store(Request $request)
|
public function store(Request $request)
|
||||||
{
|
{
|
||||||
$data = $request->validate([
|
$data = $request->validate([
|
||||||
'category_id' => 'nullable|exists:music_categories,id',
|
'category_ids' => 'nullable|array',
|
||||||
'subcategory_id' => 'nullable|exists:music_subcategories,id',
|
'category_ids.*' => 'integer|exists:music_categories,id',
|
||||||
|
'subcategory_ids' => 'nullable|array',
|
||||||
|
'subcategory_ids.*' => 'integer|exists:music_subcategories,id',
|
||||||
'name' => 'required|string|max:255',
|
'name' => 'required|string|max:255',
|
||||||
'description' => 'nullable|string',
|
'description' => 'nullable|string',
|
||||||
'image_id' => 'nullable|exists:images,id',
|
'image_id' => 'nullable|exists:images,id',
|
||||||
@@ -37,10 +46,10 @@ public function store(Request $request)
|
|||||||
'is_active' => 'nullable|boolean',
|
'is_active' => 'nullable|boolean',
|
||||||
]);
|
]);
|
||||||
|
|
||||||
// Ensure either category_id or subcategory_id is provided
|
// Ensure at least one category or subcategory is provided
|
||||||
if (!$data['category_id'] && !$data['subcategory_id']) {
|
if (empty($data['category_ids']) && empty($data['subcategory_ids'])) {
|
||||||
return response()->json([
|
return response()->json([
|
||||||
'message' => 'Either category_id or subcategory_id is required'
|
'message' => 'At least one category or subcategory is required'
|
||||||
], 422);
|
], 422);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -48,17 +57,20 @@ public function store(Request $request)
|
|||||||
|
|
||||||
$playlist = MusicPlaylist::create($data);
|
$playlist = MusicPlaylist::create($data);
|
||||||
|
|
||||||
|
$playlist->categories()->sync($data['category_ids'] ?? []);
|
||||||
|
$playlist->subcategories()->sync($data['subcategory_ids'] ?? []);
|
||||||
|
|
||||||
return response()->json([
|
return response()->json([
|
||||||
'message' => 'Playlist created successfully',
|
'message' => 'Playlist created successfully',
|
||||||
'playlist' => $playlist->load(['category', 'subcategory', 'image'])
|
'playlist' => $playlist->load(['categories', 'subcategories', 'image'])
|
||||||
], 201);
|
], 201);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function show($id)
|
public function show($id)
|
||||||
{
|
{
|
||||||
$playlist = MusicPlaylist::with([
|
$playlist = MusicPlaylist::with([
|
||||||
'category',
|
'categories',
|
||||||
'subcategory',
|
'subcategories',
|
||||||
'image',
|
'image',
|
||||||
'musics' => function($q) {
|
'musics' => function($q) {
|
||||||
$q->where('is_active', true)
|
$q->where('is_active', true)
|
||||||
@@ -100,7 +112,10 @@ public function update(Request $request, $id)
|
|||||||
$playlist = MusicPlaylist::findOrFail($id);
|
$playlist = MusicPlaylist::findOrFail($id);
|
||||||
|
|
||||||
$data = $request->validate([
|
$data = $request->validate([
|
||||||
'category_id' => 'sometimes|exists:music_categories,id',
|
'category_ids' => 'sometimes|array',
|
||||||
|
'category_ids.*' => 'integer|exists:music_categories,id',
|
||||||
|
'subcategory_ids' => 'sometimes|array',
|
||||||
|
'subcategory_ids.*' => 'integer|exists:music_subcategories,id',
|
||||||
'name' => 'sometimes|string|max:255',
|
'name' => 'sometimes|string|max:255',
|
||||||
'description' => 'nullable|string',
|
'description' => 'nullable|string',
|
||||||
'image_id' => 'nullable|exists:images,id',
|
'image_id' => 'nullable|exists:images,id',
|
||||||
@@ -114,9 +129,17 @@ public function update(Request $request, $id)
|
|||||||
|
|
||||||
$playlist->update($data);
|
$playlist->update($data);
|
||||||
|
|
||||||
|
if (array_key_exists('category_ids', $data)) {
|
||||||
|
$playlist->categories()->sync($data['category_ids'] ?? []);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (array_key_exists('subcategory_ids', $data)) {
|
||||||
|
$playlist->subcategories()->sync($data['subcategory_ids'] ?? []);
|
||||||
|
}
|
||||||
|
|
||||||
return response()->json([
|
return response()->json([
|
||||||
'message' => 'Playlist updated successfully',
|
'message' => 'Playlist updated successfully',
|
||||||
'playlist' => $playlist->load(['category', 'image'])
|
'playlist' => $playlist->load(['categories', 'subcategories', 'image'])
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
|
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
use Illuminate\Database\Eloquent\Relations\BelongsTo; // ← Add this
|
use Illuminate\Database\Eloquent\Relations\BelongsTo; // ← Add this
|
||||||
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||||
use Illuminate\Database\Eloquent\Relations\HasMany; // ← Add this
|
use Illuminate\Database\Eloquent\Relations\HasMany; // ← Add this
|
||||||
class MusicCategory extends Model
|
class MusicCategory extends Model
|
||||||
{
|
{
|
||||||
@@ -18,9 +19,9 @@ class MusicCategory extends Model
|
|||||||
'order' => 'integer',
|
'order' => 'integer',
|
||||||
];
|
];
|
||||||
|
|
||||||
public function playlists(): HasMany
|
public function playlists(): BelongsToMany
|
||||||
{
|
{
|
||||||
return $this->hasMany(MusicPlaylist::class, 'category_id');
|
return $this->belongsToMany(MusicPlaylist::class, 'music_category_playlist', 'category_id', 'playlist_id');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function subcategories(): HasMany
|
public function subcategories(): HasMany
|
||||||
@@ -45,7 +46,7 @@ public function image(): BelongsTo
|
|||||||
|
|
||||||
public function getActivePlaylistsAttribute()
|
public function getActivePlaylistsAttribute()
|
||||||
{
|
{
|
||||||
return $this->playlists()->where('is_active', true)->get();
|
return $this->playlists()->where('music_playlists.is_active', true)->get();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Total music count across all playlists and subcategories
|
// Total music count across all playlists and subcategories
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
|
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
use Illuminate\Database\Eloquent\Relations\BelongsTo; // ← Add this
|
use Illuminate\Database\Eloquent\Relations\BelongsTo; // ← Add this
|
||||||
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||||
use Illuminate\Database\Eloquent\Relations\HasMany; // ← Add this
|
use Illuminate\Database\Eloquent\Relations\HasMany; // ← Add this
|
||||||
use App\Traits\HasComments; // Add this
|
use App\Traits\HasComments; // Add this
|
||||||
use App\Traits\HasLikes;
|
use App\Traits\HasLikes;
|
||||||
@@ -14,7 +15,7 @@ class MusicPlaylist extends Model
|
|||||||
protected $table = 'music_playlists';
|
protected $table = 'music_playlists';
|
||||||
|
|
||||||
protected $fillable = [
|
protected $fillable = [
|
||||||
'category_id', 'subcategory_id','name', 'slug', 'description', 'image_id', 'order', 'is_active'
|
'name', 'slug', 'description', 'image_id', 'order', 'is_active'
|
||||||
];
|
];
|
||||||
|
|
||||||
protected $casts = [
|
protected $casts = [
|
||||||
@@ -31,14 +32,14 @@ class MusicPlaylist extends Model
|
|||||||
'is_saved',
|
'is_saved',
|
||||||
'saved_count'
|
'saved_count'
|
||||||
];
|
];
|
||||||
public function category(): BelongsTo
|
public function categories(): BelongsToMany
|
||||||
{
|
{
|
||||||
return $this->belongsTo(MusicCategory::class, 'category_id');
|
return $this->belongsToMany(MusicCategory::class, 'music_category_playlist', 'playlist_id', 'category_id');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function subcategory(): BelongsTo
|
public function subcategories(): BelongsToMany
|
||||||
{
|
{
|
||||||
return $this->belongsTo(MusicSubcategory::class, 'subcategory_id');
|
return $this->belongsToMany(MusicSubcategory::class, 'music_subcategory_playlist', 'playlist_id', 'subcategory_id');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
|
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||||
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||||
class MusicSubcategory extends Model
|
class MusicSubcategory extends Model
|
||||||
{
|
{
|
||||||
@@ -23,9 +24,9 @@ public function category(): BelongsTo
|
|||||||
return $this->belongsTo(MusicCategory::class, 'category_id');
|
return $this->belongsTo(MusicCategory::class, 'category_id');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function playlists(): HasMany
|
public function playlists(): BelongsToMany
|
||||||
{
|
{
|
||||||
return $this->hasMany(MusicPlaylist::class, 'subcategory_id');
|
return $this->belongsToMany(MusicPlaylist::class, 'music_subcategory_playlist', 'subcategory_id', 'playlist_id');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function image(): BelongsTo
|
public function image(): BelongsTo
|
||||||
@@ -35,7 +36,7 @@ public function image(): BelongsTo
|
|||||||
|
|
||||||
public function getActivePlaylistsAttribute()
|
public function getActivePlaylistsAttribute()
|
||||||
{
|
{
|
||||||
return $this->playlists()->where('is_active', true)->orderBy('order')->get();
|
return $this->playlists()->where('music_playlists.is_active', true)->orderBy('music_playlists.order')->get();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get total music count across all playlists in this subcategory
|
// Get total music count across all playlists in this subcategory
|
||||||
|
|||||||
@@ -0,0 +1,79 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Support\Facades\DB;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
|
return new class extends Migration
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Run the migrations.
|
||||||
|
*/
|
||||||
|
public function up(): void
|
||||||
|
{
|
||||||
|
Schema::create('music_category_playlist', function (Blueprint $table) {
|
||||||
|
$table->id();
|
||||||
|
$table->foreignId('playlist_id')->constrained('music_playlists')->cascadeOnDelete();
|
||||||
|
$table->foreignId('category_id')->constrained('music_categories')->cascadeOnDelete();
|
||||||
|
$table->timestamps();
|
||||||
|
|
||||||
|
$table->unique(['playlist_id', 'category_id']);
|
||||||
|
});
|
||||||
|
|
||||||
|
Schema::create('music_subcategory_playlist', function (Blueprint $table) {
|
||||||
|
$table->id();
|
||||||
|
$table->foreignId('playlist_id')->constrained('music_playlists')->cascadeOnDelete();
|
||||||
|
$table->foreignId('subcategory_id')->constrained('music_subcategories')->cascadeOnDelete();
|
||||||
|
$table->timestamps();
|
||||||
|
|
||||||
|
$table->unique(['playlist_id', 'subcategory_id']);
|
||||||
|
});
|
||||||
|
|
||||||
|
// Backfill the new pivots from the existing single columns.
|
||||||
|
if (Schema::hasColumn('music_playlists', 'category_id')) {
|
||||||
|
DB::table('music_playlists')
|
||||||
|
->whereNotNull('category_id')
|
||||||
|
->orderBy('id')
|
||||||
|
->select('id', 'category_id')
|
||||||
|
->chunk(200, function ($rows) {
|
||||||
|
$now = now();
|
||||||
|
$insert = $rows->map(fn ($row) => [
|
||||||
|
'playlist_id' => $row->id,
|
||||||
|
'category_id' => $row->category_id,
|
||||||
|
'created_at' => $now,
|
||||||
|
'updated_at' => $now,
|
||||||
|
])->all();
|
||||||
|
|
||||||
|
DB::table('music_category_playlist')->insertOrIgnore($insert);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Schema::hasColumn('music_playlists', 'subcategory_id')) {
|
||||||
|
DB::table('music_playlists')
|
||||||
|
->whereNotNull('subcategory_id')
|
||||||
|
->orderBy('id')
|
||||||
|
->select('id', 'subcategory_id')
|
||||||
|
->chunk(200, function ($rows) {
|
||||||
|
$now = now();
|
||||||
|
$insert = $rows->map(fn ($row) => [
|
||||||
|
'playlist_id' => $row->id,
|
||||||
|
'subcategory_id' => $row->subcategory_id,
|
||||||
|
'created_at' => $now,
|
||||||
|
'updated_at' => $now,
|
||||||
|
])->all();
|
||||||
|
|
||||||
|
DB::table('music_subcategory_playlist')->insertOrIgnore($insert);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverse the migrations.
|
||||||
|
*/
|
||||||
|
public function down(): void
|
||||||
|
{
|
||||||
|
Schema::dropIfExists('music_subcategory_playlist');
|
||||||
|
Schema::dropIfExists('music_category_playlist');
|
||||||
|
}
|
||||||
|
};
|
||||||
+35
@@ -0,0 +1,35 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
|
return new class extends Migration
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Run the migrations.
|
||||||
|
*/
|
||||||
|
public function up(): void
|
||||||
|
{
|
||||||
|
Schema::table('music_playlists', function (Blueprint $table) {
|
||||||
|
$table->dropForeign(['subcategory_id']);
|
||||||
|
$table->dropColumn('subcategory_id');
|
||||||
|
|
||||||
|
$table->dropForeign(['category_id']);
|
||||||
|
$table->dropColumn('category_id');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverse the migrations.
|
||||||
|
*/
|
||||||
|
public function down(): void
|
||||||
|
{
|
||||||
|
Schema::table('music_playlists', function (Blueprint $table) {
|
||||||
|
$table->foreignId('category_id')->nullable()->after('id')
|
||||||
|
->constrained('music_categories')->nullOnDelete();
|
||||||
|
$table->foreignId('subcategory_id')->nullable()->after('category_id')
|
||||||
|
->constrained('music_subcategories')->nullOnDelete();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user