根据两个字段检查现有记录

时间:2019-04-03 15:09:26

标签: mysql laravel-5.7

我正在尝试通过检查是否满足某些条件来插入一些记录。我打算做的是限制用户每天两次投票。我正在使用电子邮件和日期作为参数。

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);
        }

我现在遇到的问题是,即使我插入新电子邮件和/或当前用户,它也不会显示任何记录,因为它一直显示“您今天已投票,请等到第二天”消息用不同的电子邮件。

1 个答案:

答案 0 :(得分:2)

首先,我必须在迁移中创建一个名为“ voted_at”的字段,并根据用户的表决结果和电子邮件重新排列代码以检查现有记录

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);
                }

            }

谢谢