【问题标题】:Cannot delete model, if do application return error无法删除模型,如果应用程序返回错误
【发布时间】:2018-06-11 04:36:52
【问题描述】:

我有一些奇怪的问题。不知道是不是我做错了。

任务

简单的电子邮件验证任务。如下:

  1. 用户提交请求以获取新的电子邮件验证链接到他的收件箱。
  2. 用户点击链接被发送到他的收件箱,这导致验证页面
  3. 应用程序验证在 URL 中传递的电子邮件和验证令牌
  4. 如果电子邮件和令牌已验证。应用程序会将用户标记为已验证用户,并从电子邮件验证表中删除已记录的现有验证码/模型。

在删除现有记录之前一切正常。每次我尝试删除现有记录时,应用程序都会返回 422 Unprocessable entity 错误。

代码

控制器

public function verifyEmail(EmailVerifications $emailVerificationsRepo,
    Request $request){
    $this->emailVerificationValidator($request);
    $status = $emailVerificationsRepo->verify(
    $request->get('email'),
    $request->get('token')
    );
    if($status){
        return $this->successResponse();
    }
    return $this->errorResponse();
  }

存储库

/**
   * Verify activation code
   * @param  string $email
   * @param  string $token
   * @return boolean
   */
  public function verify($email, $token){
    $verification = $this->findWhere([
      'email' => $email,
    ])->first();
    if(!$verification){
      return false;
    }
    if(app('hash')->check($token, $verification->verification_code)){
      $user = $verification->user;
      \DB::table('email_verifications')->where('email', $email)->delete();
      $user->verifyEmail();
      return true;
    }
    return false;
  }

/**
* Delete all verification records for giving email
* @param  string $email
* @return boolean
*/
protected function deleteExisting($email){
  return $this->deleteWhere([
    'email' => $email,
  ]);
}

环境:

MacOs、Nginx、Lumen 5.5

有什么帮助吗?提前致谢

【问题讨论】:

  • 生成此令牌并存储它的代码在哪里?
  • @lagbox 我认为这无关紧要。使用随机字符创建的验证码并经过哈希处理,然后保存到数据库中。一切正常。检查哈希是否按预期通过。唯一不能做的就是删除现有记录。换句话说:如果我不从数据库中删除现有的验证码。该应用程序运行良好。

标签: php laravel eloquent lumen


【解决方案1】:

试试这个: \DB::table('email_verifications')->where('email', $email)->first()->delete();

【讨论】:

  • 查询生成器将从first返回一个stdClass对象或null
  • 然后检查是否为空不要删除
  • delete 对象上没有 delete 方法
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-12
  • 1970-01-01
  • 1970-01-01
  • 2014-05-27
  • 2013-06-12
  • 1970-01-01
相关资源
最近更新 更多