Ad

Input Facebook Data (Gender, Birthdate, Address And Bio) Into Database With Laravel Socialite

I have created a complete code which allows Facebook Login by getting Name, Email and Avatar of the user in the database by using Laravel Socialite.

I did it by writing this:

$user = User::create([
                'email' => $providerUser->getEmail(),
                'name' => $providerUser->getName(),
                'avatar' => $providerUser->getAvatar(),
                'password' => md5(rand(1,10000)),
            ]);

But for putting Gender, BirthDate and Address in the database, I tried this.

$user = User::create([
                'email' => $providerUser->getEmail(),
                'name' => $providerUser->getName(),
                'avatar' => $providerUser->getAvatar(),
                'gender' => $providerUser->getGender(),
                'bio' => $providerUser->getBio(),
                'password' => md5(rand(1,10000)),
            ]);

But, there are no functions such as getGender() or getBio(). Do you know what they are?

I have also asked necessary permissions to Facebook Driver in SocialController.php

public function redirect()
  {
      return Socialite::driver('facebook')->fields([
        'first_name', 'last_name', 'email', 'gender', 'birthday'
        ])->scopes([
            'email', 'user_birthday'
        ])->redirect();
  }

All necessary stuffs like Database Tables, Migrations, Models and a working project without Gender and Bio is ready. But I need to add more info in database. How can I do so?

I search such questions already on StackOverflow but didn't find a satisfying answer. I hope someone answers this.

Thank you in advance.

Ad

Answer

Default socialite provider for Facebook does not have the method to get gender & bio.

For more info, You can check the available methods here:

https://github.com/laravel/socialite/blob/3.0/src/Contracts/User.phphttps://github.com/laravel/socialite/blob/3.0/src/AbstractUser.php

This StackOverflow might help you:

How to get birthday gender and religion information using laravel socialite implementing facebook Api

Apart from getting the scope in redirect you also need to pass them in call back

 public function handleFacebookCallback()
{
    $facebook_user = Socialite::driver('facebook')->fields([
        'first_name', 'last_name', 'email', 'gender', 'birthday'
    ])->user();
}

Check available helper methods

https://github.com/laravel/socialite/blob/3.0/src/AbstractUser.php

  • offsetExists('gender')
  • offsetGet('gender')

These methods can help get extra info from users array

Ad
source: stackoverflow.com
Ad