validated()); $resources = $action->index(ArtistSong::query(), $query, $request->schema()); return new ArtistSongCollection($resources, $query); } /** * Store a newly created resource. * * @param StoreRequest $request * @param Artist $artist * @param Song $song * @param StoreAction $action * @return ArtistSongResource */ 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()); } /** * Display the specified resource. * * @param ShowRequest $request * @param Artist $artist * @param Song $song * @param ShowAction $action * @return ArtistSongResource */ 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); } /** * Update the specified resource. * * @param UpdateRequest $request * @param Artist $artist * @param Song $song * @param UpdateAction $action * @return ArtistSongResource */ 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); } /** * Remove the specified resource. * * @param Artist $artist * @param Song $song * @param DestroyAction $action * @return JsonResponse */ 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()}'.", ]); } }