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

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

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

最佳答案

首先,我必须在迁移中创建一个名为“voted_at”的字段,并重新排列代码以根据用户的 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);
                }

            }

谢谢

关于mysql - 根据两个字段检查现有记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55498489/

相关文章:

php - 如何将数组(不同)插入到 mySQL 中?

php - 如何根据发布日期对数据进行排序?

php - Laravel 作业链

php - 如何在 laravel 5.7 中测试 mongo 连接?

具有多个参数的 Laravel 5.7 自定义 URL

php artisan key :generate doesn't do anything

c# - 对象不能从 DBNull 转换为其他类型? C#

mysql - 使用同一个表中的子查询更新

javascript - 当一个词的一部分在表格数据中时突出显示表格行数据输出MYSQL in HTML CSS PHP

使用Guard的Laravel Passport多重身份验证