feat: add tags

This commit is contained in:
2025-11-26 09:34:33 +03:30
parent fef81022fa
commit bebf685f32
5 changed files with 79 additions and 6 deletions
+41 -4
View File
@@ -4,6 +4,8 @@
use App\Models\Media;
use App\Models\Category;
use App\Models\Tag;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
@@ -24,6 +26,9 @@ public function store(Request $request)
'file' => 'nullable|mimes:mp3,wav,mp4,mov|max:50480',
'external_url' => 'nullable|string',
'visibility' => 'nullable|in:public,private',
'tags' => 'nullable|array',
'tags.*' => 'string',
]);
$categoryId = $data['category_id'] ?? null;
@@ -51,17 +56,26 @@ public function store(Request $request)
'duration' => $data['duration'] ?? null,
'visibility' => $data['visibility'] ?? 'public',
]);
if (!empty($data['tags'])) {
$tagIds = [];
foreach ($data['tags'] as $tagName) {
$tag = Tag::firstOrCreate(['name' => $tagName]);
$tagIds[] = $tag->id;
}
$media->tags()->sync($tagIds);
}
return response()->json([
'message' => 'Media created successfully',
'media' => $media->load(['image', 'category']),
'media' => $media->load(['image', 'category' , 'tags']),
]);
}
// GET all visible media
public function index()
{
$media = Media::with(['image', 'category', 'myNote'])
$media = Media::with(['image', 'category', 'myNote' , 'tags'])
->where('visibility', 'public')
->orWhere('user_id', auth()->id())
->get();
@@ -69,6 +83,17 @@ public function index()
return response()->json($media);
}
public function show($id)
{
$media = Media::with(['image', 'category', 'myNote' , 'tags'])
->where('id', $id)
->where('visibility', 'public')
->orWhere('user_id', auth()->id())
->firstOrFail();
return response()->json($media);
}
// UPDATE media
public function update(Request $request, $id)
{
@@ -90,6 +115,8 @@ public function update(Request $request, $id)
'file' => 'nullable|mimes:mp3,wav,mp4,mov|max:50480',
'external_url' => 'nullable|string',
'visibility' => 'nullable|in:public,private',
'tags' => 'nullable|array',
'tags.*' => 'string',
]);
// --- handle auto-create category ---
@@ -113,10 +140,20 @@ public function update(Request $request, $id)
// --- update media ---
$media->update($data);
if (isset($data['tags'])) {
$tagIds = [];
foreach ($data['tags'] as $tagName) {
$tag = Tag::firstOrCreate(['name' => $tagName]);
$tagIds[] = $tag->id;
}
$media->tags()->sync($tagIds);
}
return response()->json([
'message' => 'Media updated successfully',
'media' => $media->load(['image', 'category']),
'media' => $media->load(['image', 'category' , 'tags']),
]);
}
@@ -145,7 +182,7 @@ public function saveMedia($id)
// GET saved
public function saved()
{
return auth()->user()->savedMedia()->with(['image','category' , 'myNote'])->get();
return auth()->user()->savedMedia()->with(['image','category' , 'myNote' , 'tags'])->get();
}
public function storeNote(Request $request, $mediaId)
+4 -1
View File
@@ -28,7 +28,10 @@ public function category()
{
return $this->belongsTo(Category::class);
}
public function tags()
{
return $this->belongsToMany(Tag::class, 'media_tag');
}
public function notes()
{
return $this->morphMany(Note::class, 'noteable');
+4
View File
@@ -12,5 +12,9 @@ public function questions()
{
return $this->belongsToMany(Question::class);
}
public function media()
{
return $this->belongsToMany(Media::class);
}
}