0

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.

Kumar Abhirup
  • 197
  • 4
  • 16
  • There isn’t a specific getter for every single possible field ... but you don’t need one in the first place. Check https://developers.facebook.com/docs/php/howto/example_retrieve_user_profile for an easy example how this can be handled. – CBroe Mar 21 '18 at 10:26

1 Answers1

2

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.php https://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

Ajit Bohra
  • 373
  • 3
  • 9