feat: add imag_id to music
This commit is contained in:
@@ -16,6 +16,7 @@ public function store(Request $request)
|
|||||||
'artist' => 'nullable|string|max:255',
|
'artist' => 'nullable|string|max:255',
|
||||||
'file' => 'required|mimes:mp3,wav,ogg|max:10240', // max 10MB
|
'file' => 'required|mimes:mp3,wav,ogg|max:10240', // max 10MB
|
||||||
'type' => 'nullable|in:public,private',
|
'type' => 'nullable|in:public,private',
|
||||||
|
'image_id' => 'nullable|exists:images,id',
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$path = $request->file('file')->store('music', 'public');
|
$path = $request->file('file')->store('music', 'public');
|
||||||
@@ -26,11 +27,12 @@ public function store(Request $request)
|
|||||||
'artist' => $data['artist'] ?? null,
|
'artist' => $data['artist'] ?? null,
|
||||||
'file_path' => $path,
|
'file_path' => $path,
|
||||||
'type' => $data['type'] ?? 'private',
|
'type' => $data['type'] ?? 'private',
|
||||||
|
'image_id' => $data['image_id'] ?? null,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
return response()->json([
|
return response()->json([
|
||||||
'message' => 'Music uploaded successfully',
|
'message' => 'Music uploaded successfully',
|
||||||
'music' => $music,
|
'music' => $music->load('image'),
|
||||||
'url' => asset('storage/' . $path),
|
'url' => asset('storage/' . $path),
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
@@ -46,13 +48,14 @@ public function all()
|
|||||||
})
|
})
|
||||||
->get();
|
->get();
|
||||||
|
|
||||||
return response()->json($music->map(fn($m) => [
|
$music = Music::with('image') // eager load image
|
||||||
'id' => $m->id,
|
->where('type', 'public')
|
||||||
'title' => $m->title,
|
->orWhere(function($query) use ($userId) {
|
||||||
'artist' => $m->artist,
|
$query->where('type', 'private')->where('user_id', $userId);
|
||||||
'type' => $m->type,
|
})
|
||||||
'url' => asset('storage/' . $m->file_path),
|
->get();
|
||||||
]));
|
|
||||||
|
return response()->json($music);
|
||||||
}
|
}
|
||||||
|
|
||||||
// ✅ Update music
|
// ✅ Update music
|
||||||
@@ -65,6 +68,7 @@ public function update(Request $request, $id)
|
|||||||
'artist' => 'nullable|string|max:255',
|
'artist' => 'nullable|string|max:255',
|
||||||
'type' => 'nullable|in:public,private',
|
'type' => 'nullable|in:public,private',
|
||||||
'file' => 'nullable|mimes:mp3,wav,ogg|max:10240',
|
'file' => 'nullable|mimes:mp3,wav,ogg|max:10240',
|
||||||
|
'image_id' => 'nullable|exists:images,id',
|
||||||
]);
|
]);
|
||||||
|
|
||||||
if ($request->hasFile('file')) {
|
if ($request->hasFile('file')) {
|
||||||
@@ -73,14 +77,13 @@ public function update(Request $request, $id)
|
|||||||
$music->file_path = $path;
|
$music->file_path = $path;
|
||||||
}
|
}
|
||||||
|
|
||||||
$music->title = $data['title'] ?? $music->title;
|
$music->fill($data);
|
||||||
$music->artist = $data['artist'] ?? $music->artist;
|
|
||||||
$music->type = $data['type'] ?? $music->type;
|
|
||||||
$music->save();
|
$music->save();
|
||||||
|
|
||||||
|
|
||||||
return response()->json([
|
return response()->json([
|
||||||
'message' => 'Music updated successfully',
|
'message' => 'Music updated successfully',
|
||||||
'music' => $music,
|
'music' => $music->load('image'),
|
||||||
'url' => asset('storage/' . $music->file_path),
|
'url' => asset('storage/' . $music->file_path),
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|||||||
+10
-1
@@ -14,6 +14,7 @@ class Music extends Model
|
|||||||
'artist',
|
'artist',
|
||||||
'file_path',
|
'file_path',
|
||||||
'type', // public or private
|
'type', // public or private
|
||||||
|
'image_id',
|
||||||
];
|
];
|
||||||
|
|
||||||
// Relation to user (optional)
|
// Relation to user (optional)
|
||||||
@@ -21,10 +22,18 @@ public function user()
|
|||||||
{
|
{
|
||||||
return $this->belongsTo(User::class);
|
return $this->belongsTo(User::class);
|
||||||
}
|
}
|
||||||
|
protected $appends = ['url', 'image_url'];
|
||||||
// Accessor for full URL
|
// Accessor for full URL
|
||||||
public function getUrlAttribute()
|
public function getUrlAttribute()
|
||||||
{
|
{
|
||||||
return asset('storage/' . $this->file_path);
|
return asset('storage/' . $this->file_path);
|
||||||
}
|
}
|
||||||
|
public function image()
|
||||||
|
{
|
||||||
|
return $this->belongsTo(Image::class, 'image_id');
|
||||||
|
}
|
||||||
|
public function getImageUrlAttribute()
|
||||||
|
{
|
||||||
|
return $this->image ? asset('storage/' . $this->image->path) : null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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', function (Blueprint $table) {
|
||||||
|
$table->unsignedBigInteger('image_id')->nullable()->after('file_path');
|
||||||
|
|
||||||
|
// add foreign key constraint with cascade on delete set null
|
||||||
|
$table->foreign('image_id')
|
||||||
|
->references('id')
|
||||||
|
->on('images')
|
||||||
|
->onDelete('set null');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverse the migrations.
|
||||||
|
*/
|
||||||
|
public function down(): void
|
||||||
|
{
|
||||||
|
Schema::table('music', function (Blueprint $table) {
|
||||||
|
$table->dropForeign(['image_id']);
|
||||||
|
$table->dropColumn('image_id');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user