public function run()
 {
     DB::table('used_codes')->delete();
     UsedCode::create(['code' => '1111', 'user_id' => 1]);
     UsedCode::create(['code' => '2222', 'user_id' => 1]);
     UsedCode::create(['code' => '3333', 'user_id' => 1]);
     UsedCode::create(['code' => '4444', 'user_id' => 1]);
 }
 public function postCode(Request $request)
 {
     //validate input
     $this->validate($request, ['code' => 'required|size:10']);
     // search for code
     $code = WinningCode::withTrashed()->where('code', $request->code)->first();
     $user = Auth::user();
     $username = $user->name;
     $userPoints = $user->points;
     $rewards = Reward::orderBy('created_at', 'desc')->take(8)->get();
     //check if code is a bonus code
     if ($code && $code->used == 0 && $code->deleted_at == null) {
         $title = 'Dashboard';
         $usedCode = new UsedCode();
         $winner = new Winner();
         $codeValue = $code->value;
         //check if user used bonus code
         if ($code->bonus == 1) {
             if ($user->bonus == 0) {
                 // add points to account + save that user used bonus code
                 $userPoints = $userPoints + $codeValue;
                 $user->points = $userPoints;
                 $user->bonus = 1;
                 $usedCode->code = $code->code;
                 $usedCode->value = $code->value;
                 $usedCode->user()->associate($user);
                 // new winner
                 $winner->name = $user->name;
                 $winner->value = $code->value;
                 $winner->period = Period::where('running', 1)->first()->id;
                 $winner->user()->associate($user);
                 $winner->save();
                 $user->save();
                 $usedCode->save();
                 $success = true;
                 return view('dashboard.code.result', compact('title', 'username', 'userPoints', 'rewards', 'success', 'codeValue'));
             } else {
                 $success = false;
                 $codeError = 'You have already used your free code.';
                 return view('dashboard.code.result', compact('title', 'username', 'userPoints', 'rewards', 'codeError', 'success'));
             }
         }
         // add points to account + save that code is used
         $code->used = 1;
         $code->save();
         $userPoints = $userPoints + $codeValue;
         $user->points = $userPoints;
         $usedCode->code = $code->code;
         $usedCode->value = $code->value;
         $usedCode->user()->associate($user);
         // new winner
         $winner->name = $user->name;
         $winner->value = $code->value;
         $winner->period = Period::where('running', 1)->first()->id;
         $winner->user()->associate($user);
         $winner->save();
         $user->save();
         $usedCode->save();
         $success = true;
         return view('dashboard.code.result', compact('title', 'username', 'userPoints', 'rewards', 'success', 'codeValue'));
     } else {
         //check if code is used before
         if ($code && $code->used == 1 && $code->deleted_at == null) {
             $success = false;
             $codeError = 'This code is alreay used and can only be used once';
             return view('dashboard.code.result', compact('title', 'username', 'userPoints', 'rewards', 'codeError', 'success'));
             //check if code is deleted
         } elseif ($code && $code->deleted_at != null) {
             $success = false;
             $codeError = 'This code is suspended';
             return view('dashboard.code.result', compact('title', 'username', 'userPoints', 'rewards', 'codeError', 'success'));
             // code does not exist
         } else {
             $success = false;
             $codeError = 'This code does not exist, did you make a mistake?';
             return view('dashboard.code.result', compact('title', 'username', 'userPoints', 'rewards', 'codeError', 'success'));
         }
     }
 }