photos()->orderBy('created_at', 'desc')->get(); return PhotoResource::collection($photos); } /** * Store the Photo. * * @param Request $request * @param Contact $contact * @return PhotoResource */ public function store(Request $request, Contact $contact): PhotoResource { $photo = app(UploadPhoto::class)->execute([ 'account_id' => auth()->user()->account_id, 'contact_id' => $contact->id, 'photo' => $request->photo, ]); return new PhotoResource($photo); } /** * Delete the Photo. * Also, if this photo was the current avatar of the contact, change the * avatar to the default one. * * @param Request $request * @param Contact $contact * @param Photo $photo * @return null|\Illuminate\Http\JsonResponse */ public function destroy(Request $request, Contact $contact, Photo $photo) { $data = [ 'account_id' => auth()->user()->account_id, 'photo_id' => $photo->id, ]; try { app(DestroyPhoto::class)->execute($data); } catch (\Exception $e) { return $this->respondNotFound(); } if ($contact->avatar_source == 'photo' && $contact->avatar_photo_id == $photo->id) { app(UpdateAvatar::class)->execute([ 'account_id' => auth()->user()->account_id, 'contact_id' => $contact->id, 'source' => 'adorable', ]); } return $this->respondObjectDeleted($photo->id); } }