app/Http/Controllers/UserController.php
/**
     * @param Request $request
     * @return JsonResponse
     * @throws ValidationException
     */
@param Request $request
@return JsonResponse
@throws ValidationException
@param Request $request @return JsonResponse @throws ValidationException
public function updateMyDestinations(Request $request): JsonResponse
    {
        $user = Auth::user();
        $rules = [
            'visited' => 'exists:destinations,id',
            'wantsToGo' => 'exists:destinations,id'
        ];
        $this->validate($request, $rules);
        $user
            ->flags()
            ->where('flaggable_type', Destination::class)
            ->where('flag_type', 'havebeen')
            ->delete();
        collect($request->post('visited'))->each(function ($id) use ($user) {
            $user->flags()->create([
                'flaggable_type' => Destination::class,
                'flaggable_id' => $id,
                'flag_type' => 'havebeen'
            ]);
        });
        $user
            ->flags()
            ->where('flaggable_type', Destination::class)
            ->where('flag_type', 'wantstogo')
            ->delete();
        collect($request->post('wantsToGo'))->each(function ($id) use ($user) {
            $user->flags()->create([
                'flaggable_type' => Destination::class,
                'flaggable_id' => $id,
                'flag_type' => 'wantstogo'
            ]);
        });
        return response()->json('success');
    }
public function updateMyDestinations ( Request $ request ) : JsonResponse { $ user = Auth :: user ( ) ; $ rules = [ 'visited' => 'exists:destinations,id' , 'wantsToGo' => 'exists:destinations,id' ] ; $ this -> validate ( $ request , $ rules ) ; $ user -> flags ( ) -> where ( 'flaggable_type' , Destination :: class ) -> where ( 'flag_type' , 'havebeen' ) -> delete ( ) ; collect ( $ request -> post ( 'visited' ) ) -> each ( function ( $ id ) use ( $ user ) { $ user -> flags ( ) -> create ( [ 'flaggable_type' => Destination :: class , 'flaggable_id' => $ id , 'flag_type' => 'havebeen' ] ) ; } ) ; $ user -> flags ( ) -> where ( 'flaggable_type' , Destination :: class ) -> where ( 'flag_type' , 'wantstogo' ) -> delete ( ) ; collect ( $ request -> post ( 'wantsToGo' ) ) -> each ( function ( $ id ) use ( $ user ) { $ user -> flags ( ) -> create ( [ 'flaggable_type' => Destination :: class , 'flaggable_id' => $ id , 'flag_type' => 'wantstogo' ] ) ; } ) ; return response ( ) -> json ( 'success' ) ; }
@param Request $request
 @return JsonResponse
 @throws ValidationException
@param Request $request @return JsonResponse @throws ValidationException