seamons seamons - 1 year ago 259
SQL Question

Laravel 5 Socialite Duplicate entry

I implement socialite together with manual registration where register using facebook is a user's option. but i noticed that if the user registered first with the manual registration and logout then came back and register using facebook it produce an error

SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry"

here is my current UserRepository

namespace App\Repositories;
use App\User;
class UserRepository{
public function findByUsernameOrCreate($userData)
return User::firstOrCreate([
'first_name' => $userData->first_name,
'last_name' => $userData->last_name,
'email' => $userData->email

Answer Source

I recently had this issue. I was testing a pre registered user's ability to login with facebook and have their avatar and email updated when they did.

Assuming you can check for existing (non facebook) user by their email address.

$user = User::where('email', $userData->email)->first();

if ($user){

  // Update existing user with facebook data.
  $user->email = $userData->email;
  // Any other fields you want to update.
  $user->avatar = $userData->avatar;


  return $user;

Then your original function here.