从PDOException返回特定项目$ e

时间:2015-07-29 05:47:40

标签: php mysql pdo laravel-5

我在我的routes.php中尝试捕捉它工作正常。

Route::get('{provider}/login', function ($provider) {
try
{
OAuth::login($provider, function ($user, $userDetails) 
{
    $user->email = $userDetails->email;
    $user->name = $userDetails->firstName . ' ' . $userDetails->lastName;
    $user->first_name = $userDetails->firstName;
    $user->last_name = $userDetails->lastName;
    $user->picture = $userDetails->imageUrl;
        $user->about = $userDetails->summary;
    $user->save();
    });

    return view('home');

} 
catch (ApplicationRejectedException $e) 
{
    // User rejected application
} 
catch (InvalidAuthorizationCodeException $e) 
{
    // Authorization was attempted with invalid
    // code,likely forgery attempt
}
catch(PDOException $err)
{
    return redirect()->guest('home');
}   
});

我想要做的是从PDOException $ err消息中提取特定项。

如果我尝试登录,我知道会因PDO异常而失败,而且我" dd"我得到的错误:

QueryException {#397 
#sql: "insert into `users` (`email`, `name`, `first_name`, `last_name`, `picture`, `about`, `facebook_id`, `updated_at`, `created_at`) values (?, ?, ?, ?, ?, ?, ?, ?, ?)"
#bindings: array:9 [?
0 => "mhluzi@email.com"
etc....

如何获取电子邮件地址?

我尝试了各种

$err->binding[0] 

等没有运气。

这有可能吗?

2 个答案:

答案 0 :(得分:0)

如果返回以下内容:

catch(PDOException $err)
{
    $thisMessage=$err->getMessage();
    dd($thisMessage);
}

整个字符串都可用:

"SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'mhluzi@email.com' for key 'users_email_unique' (SQL: insert into `users` 

所以我所做的是用“*”替换“'”

然后能够访问我需要的东西。

catch(PDOException $err)
{
    $thisMessage = str_replace("'", "*", $err->getMessage());
    if(preg_match_all('/\*(.*?)\*/',$thisMessage,$thisMatch)) 
    {
        dd($thisMatch[1][0]); //first occurrence which is what we want
    }
}

输出:

"mhluzi@email.com"

答案 1 :(得分:-1)

确定。所以我发现如果我回来了:

catch(PDOException $err)
{
    $thisMessage=$err->getMessage();
    dd($thisMessage);
}

我得到了整个字符串:

"SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'mhluzi@email.com' for key 'users_email_unique' (SQL: insert into `users` 

所以我所做的就是更换" ' "用" *"

然后能够访问我需要的内容。

catch(PDOException $err)
{
    $thisMessage = str_replace("'", "*", $err->getMessage());
    if(preg_match_all('/\*(.*?)\*/',$thisMessage,$thisMatch)) 
    {
        dd($thisMatch[1][0]); //first occurrence which is what we want
    }
}

输出:

"mhluzi@email.com"

我希望能帮到别人!