validated()); $resources = $action->index(ArtistSong::query(), $query, $request->schema()); return new ArtistSongCollection($resources, $query); } /** * @param StoreAction $action */ public function store(StoreRequest $request, Artist $artist, Song $song, StoreAction $action): ArtistSongResource { $validated = array_merge( $request->validated(), [ ArtistSong::ATTRIBUTE_ARTIST => $artist->getKey(), ArtistSong::ATTRIBUTE_SONG => $song->getKey(), ] ); $artistSong = $action->store(ArtistSong::query(), $validated); return new ArtistSongResource($artistSong, new Query()); } public function show(ShowRequest $request, Artist $artist, Song $song, ShowAction $action): ArtistSongResource { $artistSong = ArtistSong::query() ->where(ArtistSong::ATTRIBUTE_ARTIST, $artist->getKey()) ->where(ArtistSong::ATTRIBUTE_SONG, $song->getKey()) ->firstOrFail(); $query = new Query($request->validated()); $show = $action->show($artistSong, $query, $request->schema()); return new ArtistSongResource($show, $query); } public function update(UpdateRequest $request, Artist $artist, Song $song, UpdateAction $action): ArtistSongResource { $artistSong = ArtistSong::query() ->where(ArtistSong::ATTRIBUTE_ARTIST, $artist->getKey()) ->where(ArtistSong::ATTRIBUTE_SONG, $song->getKey()) ->firstOrFail(); $query = new Query($request->validated()); $updated = $action->update($artistSong, $request->validated()); return new ArtistSongResource($updated, $query); } public function destroy(Artist $artist, Song $song, DestroyAction $action): JsonResponse { $artistSong = ArtistSong::query() ->where(ArtistSong::ATTRIBUTE_ARTIST, $artist->getKey()) ->where(ArtistSong::ATTRIBUTE_SONG, $song->getKey()) ->firstOrFail(); $action->destroy($artistSong); return new JsonResponse([ 'message' => "Song '{$song->getName()}' has been detached from Artist '{$artist->getName()}'.", ]); } }