Laravel 5 SQLSTATE [23000] users_email_unique

时间:2015-07-24 05:20:01

标签: php facebook laravel laravel-5

我正在使用laravel 5在我的网站上实施社交认证。

我成功登录了几个用户,但由于一些非常奇怪的原因,它不再起作用了..

当我尝试登录新用户时出现此错误:

QueryException in Connection.php line 624:
SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '' for key 'users_email_unique' (SQL: insert into `users` (`name`, `profile_picture`, `facebook_id`) values (Hwan, https://graph.facebook.com/v2.4/1701160536770162/picture1701160536770162type=normal, ?))

但此用户之前从未在数据库中注册过!!

我试过与其他用户一样,都是这样......

但是如果我从数据库中删除现有的FB用户并再试一次,那就可以了!!

这是我的控制器:

class AccountController extends Controller {
/**
 * Redirect the app to the social provider
 *
 * @return SNS token and user data
 */
public function redirectToProvider($provider) {
    return Socialize::with($provider)->redirect();
}
/**
 * Callback handler
 *
 * @return redirect to index
 */
public function handleProviderCallback($provider) {
    $user   = Socialize::with($provider)->user();

    // get the sns user data
    $id         = $user->getId();
    $name   = $user->getName();
    $avatar = $user->getAvatar();

    // get the user provider id form the DB
    $users = DB::table('users')->where($provider.'_id', $id)->get();

    // check if the record exists
    if(empty($users)){
        DB::table('users')->insert(
            ['name' => $name,'profile_picture' => $avatar,$provider.'_id' => $id]
        );
        $users = DB::table('users')->where($provider.'_id', $id)->get();
    }

    foreach ($users as $user)
    {
        $userID = $user->id;
    }
    Auth::loginUsingId($userID);
    {
        return redirect()->to('home');
    }
}

}

我的路线:

Route::get('connect/{provider}', 'AccountController@redirectToProvider');
Route::get('account/{provider}', 'AccountController@handleProviderCallback');

我的用户架构:

 Schema::create('users', function(Blueprint $table)
    {
        $table->increments('id');
        $table->string('name');
        $table->string('email')->unique();
        $table->string('password', 60);
        $table->string('profile_picture');
        $table->text('facebook_id');
        $table->rememberToken();
        $table->timestamps();
    });

非常感谢任何帮助

谢谢

1 个答案:

答案 0 :(得分:1)

您的电子邮件字段有唯一约束,但您似乎没有插入电子邮件地址。插入一个没有电子邮件地址的用户后,没有电子邮件地址就无法注册其他用户。

您的数据库的电子邮件列中将无法使用两个空字符串。