Abdulkabir Ojulari
Abdulkabir Ojulari

Reputation: 1467

Checking existing record based on two fields

I am trying to insert some record by checking if certain conditions are met. What I am planning to do is to restrict the users from voting twice daily. I am using email and date as parameters.

public function store(Request $request)
        {
            $this->validate($request, [

                'flavour' => 'required|string',
                'email' => 'required|email',
                'lastname'=> 'required|string',
                'firstname' => 'required|string'

            ]);

            $vote = new Vote();
            //$currentDate = Carbon::now()->toDateString();
            $today = Carbon::now()->format('Y-M-D');

                $dup = Vote::where(['email' => $request->email, 'created_at' => $today ])->get();
                if (!$dup)
                {
                $vote->firstname = $request->firstname;
                $vote->lastname = $request->lastname;
                $vote->email   = $request->email;
                $vote->flavour = $request->flavour;
                $vote->voter_ip = $request->ip();
                }
                else {
                    return response()->json([
                        'message' => 'You have voted today, please wait till another day!'
                   ]);
                }



              if (auth()->user()->votes()->save($vote))
                return response()->json([
                    'success' => true,
                    'data' => $vote->toArray()
                ]);
            else
                return response()->json([
                    'success' => false,
                    'message' => 'Vote could not be added'
                ], 500);
        }

The problem I am encountering now is, it doesn't take any record as it keeps displaying the message "You have voted today, please wait till another day", even if I insert a new email and/or a current user with different emails.

Upvotes: 2

Views: 29

Answers (1)

Abdulkabir Ojulari
Abdulkabir Ojulari

Reputation: 1467

First, I had to create a field in my migration called "voted_at" and rearranged the code to check for the existing record based on the voted_at and email of the user

public function store(Request $request)
            {
                $this->validate($request, [

                    'flavour' => 'required|string',
                    'email' => 'required|email',
                    'lastname'=> 'required|string',
                    'firstname' => 'required|string'

                ]);
                $today = Carbon::now()->toDateString();
                $dup = Vote::where(['email' => $request->email, 'voted_at' => $today ])->exists();
                    if ($dup){
                        return response()->json([
                            'data' => $dup,
                            'message' => 'You have voted today, please wait till another day!'
                       ]);
                    }
                else {
                    $vote = new Vote();
                    $vote->firstname = $request->firstname;
                    $vote->lastname = $request->lastname;
                    $vote->email   = $request->email;
                    $vote->flavour = $request->flavour;
                    $vote->voter_ip = $request->ip();
                    $vote->voted_at =  $today;
                if (auth()->user()->votes()->save($vote))
                    return response()->json([
                        'success' => true,
                        'data' => $vote->toArray()
                    ]);
                else
                    return response()->json([
                        'success' => false,
                        'message' => 'Vote could not be added'
                    ], 500);
                }

            }

Thanks

Upvotes: 2

Related Questions