where('organisation_id', $organisation->id) ->with(['defaultGenre', 'agentCompany']); if ($request->boolean('with_trashed')) { $query->withTrashed(); } if ($request->boolean('trashed_only')) { $query->onlyTrashed(); } if ($request->filled('search')) { $term = '%'.$request->string('search').'%'; $query->where(function ($q) use ($term): void { $q->where('name', 'like', $term)->orWhere('slug', 'like', $term); }); } if ($request->filled('genre_id')) { $query->where('default_genre_id', $request->string('genre_id')); } if ($request->filled('agent_company_id')) { $query->where('agent_company_id', $request->string('agent_company_id')); } return ArtistResource::collection($query->orderBy('name')->paginate(50)); } public function show(Organisation $organisation, Artist $artist): JsonResponse { Gate::authorize('view', $artist); $artist->loadMissing(['defaultGenre', 'agentCompany', 'contacts']); return $this->success(ArtistResource::make($artist)); } public function store(CreateArtistRequest $request, Organisation $organisation): JsonResponse { Gate::authorize('create', [Artist::class, $organisation]); try { $artist = $this->service->create($organisation, $request->validated()); } catch (DuplicateArtistException $e) { return $this->error('Duplicate artist name.', 409, [ 'duplicate_artist_id' => $e->existing->id, ]); } return $this->created(ArtistResource::make($artist->load(['defaultGenre', 'agentCompany']))); } public function update(UpdateArtistRequest $request, Organisation $organisation, Artist $artist): JsonResponse { Gate::authorize('update', $artist); $artist = $this->service->update($artist, $request->validated()); return $this->success(ArtistResource::make($artist->load(['defaultGenre', 'agentCompany']))); } public function destroy(Organisation $organisation, Artist $artist): JsonResponse { if (! Gate::check('delete', $artist)) { return $this->forbidden('Cannot delete artist with active engagements.'); } $this->service->softDelete($artist); return response()->json(null, 204); } public function restore(Organisation $organisation, string $artist): JsonResponse { $model = Artist::withTrashed()->findOrFail($artist); Gate::authorize('restore', $model); $this->service->restore($model); return $this->success(ArtistResource::make($model->fresh())); } }