middleware('auth:sanctum')->except(['index', 'show']); } public function index(Request $request): JsonResponse { $perPage = (int) $request->get('per_page', 100); $items = EdiBand::query() ->with('bands') // eager load pokud chceš mít vazby ->orderBy('value') ->paginate($perPage); return response()->json($items); } public function store(Request $request): JsonResponse { $this->authorize('create', EdiBand::class); $data = $request->validate([ 'value' => ['required', 'string', 'max:255'], ]); $relations = $request->validate([ 'band_ids' => ['sometimes', 'array'], 'band_ids.*' => ['integer', 'exists:bands,id'], ]); $item = EdiBand::create($data); if (array_key_exists('band_ids', $relations)) { $item->bands()->sync($relations['band_ids']); } return response()->json($item, 201); } public function show(EdiBand $edi_band): JsonResponse { $edi_band->load('bands'); return response()->json($edi_band); } public function update(Request $request, EdiBand $edi_band): JsonResponse { $this->authorize('update', $edi_band); $data = $request->validate([ 'value' => ['sometimes', 'string', 'max:255'], ]); $relations = $request->validate([ 'band_ids' => ['sometimes', 'array'], 'band_ids.*' => ['integer', 'exists:bands,id'], ]); $edi_band->fill($data); $edi_band->save(); if (array_key_exists('band_ids', $relations)) { $edi_band->bands()->sync($relations['band_ids']); } return response()->json($edi_band); } public function destroy(EdiBand $edi_band): JsonResponse { $this->authorize('delete', $edi_band); $edi_band->delete(); return response()->json(null, 204); } }