public function getIndex()
 {
     $title = 'public';
     // get 8 most popular online rewards
     $rewards = Reward::orderBy('claimed', 'desc')->take(8)->get();
     // get last 5 winners
     $winners = Winner::orderBy('created_at', 'desc')->take(5)->get();
     // check if competition is running, has yet to start or has ended
     $time = Carbon::now()->toDateTimeString();
     $runningPeriod = Period::where('running', 1)->get();
     $firstPeriod = Period::orderBy('id', 'asc')->first();
     if (count($runningPeriod) == 1) {
         $competition['running'] = true;
         $competition['message'] = 'Enter your codes here to get your well-deserved points';
     } else {
         $competition['running'] = false;
         if ($time < $firstPeriod->start) {
             $competition['message'] = 'The competition has yet to start';
         } else {
             $competition['message'] = 'The competition has ended';
         }
     }
     // if logged in go to dashboard
     if (Auth::check()) {
         $user = Auth::user();
         $username = $user->name;
         $userPoints = $user->points;
         $title = 'Dashboard';
         $rewards = Reward::all();
         return view('dashboard.home', compact('competition', 'title', 'username', 'rewards', 'userPoints'));
     }
     return view('public.home', compact('competition', 'title', 'rewards', 'winners'));
 }
 public function getRegister()
 {
     $rewards = Reward::orderBy('claimed', 'desc')->take(8)->get();
     if (Auth::check()) {
         return view('dashboard.home');
     }
     $code = WinningCode::where('bonus', 1)->first()->code;
     return view('auth.register', compact('code', 'rewards'));
 }
 public function postUnlock(Request $request)
 {
     $title = 'Dashboard unlock reward';
     $rewardId = $request->id;
     // get reward for detail page
     $reward = Reward::find($rewardId);
     // get 8 most popular rewards
     $rewards = Reward::orderBy('claimed', 'desc')->take(8)->get();
     $user = Auth::user();
     $username = $user->name;
     $userPoints = $user->points;
     $message = '';
     // check if user has enough points
     if ($userPoints >= $reward->value) {
         // calculate new points
         $user->points = $userPoints - $reward->value;
         $userPoints = $user->points;
         // add reward to rewards of user
         $user->rewards()->attach($rewardId);
         $reward->claimed = $reward->claimed + 1;
         $reward->save();
         $user->save();
         $message = 'Congratulations! You bought it!';
         return view('dashboard.reward.unlock', compact('title', 'username', 'reward', 'userPoints', 'rewards', 'message'));
     } else {
         // not enough points to unlock
         $message = 'You dont have enough points!';
         return view('dashboard.reward.unlock', compact('title', 'username', 'reward', 'userPoints', 'rewards', 'message'));
     }
 }
 public function restoreReward($id)
 {
     if (Auth::user()->admin) {
         $title = 'admin rewards';
         // search for reward
         $reward = Reward::withTrashed()->where('id', $id)->get()->first();
         $reward->restore();
         // get all online rewards
         $rewards = Reward::orderBy('created_at', 'desc')->get();
         // get all deleted rewards
         $deletedRewards = Reward::onlyTrashed()->get();
         return view('admin.rewards', compact('title', 'rewards', 'deletedRewards'));
     } else {
         return redirect('dashboard');
     }
 }