haypro
haypro

Reputation: 93

Laravel 5.1 - Socialite - array key has value, but returns nothing

if a user is logged in using facebook, I want to store that info in the users table, for further use of the app, but some values are not visible.

When logged in using facebook, I got the user info using this code (imported "Kint" to show these values):

    $user = Socialite::driver('facebook')->user();
    s($user);

the output of this script:

Laravel\Socialite\Two\User (8) (
    public token -> string (197) "facebook token .."
    public id -> string (17) "user id .."
    public nickname -> NULL
    public name -> string (11) "my name"
    public email -> string (21) "[email protected]"
    public avatar -> string (69) "https://graph.facebook.com/v2.5/my_id/picture?type=normal"
    public user -> array (6) [
        'first_name' => string (5) "first name"
        'last_name' => string (5) "last name"
        'email' => string (21) "[email protected]"
        'gender' => string (4) "male"
        'verified' => bool TRUE
        'id' => string (17) "my id"
    ]
    public avatar_original -> string (68) "https://graph.facebook.com/v2.5/my_id/picture?width=1920"
)

I want to put some of these values in a table using this script:

DB::table('users')->insert(
        ['user_id' => array_get($user, 'id'),
        'name' => array_get($user, 'name'),
        'first_name' => array_get($user, 'user.first_name'),
        'last_name' => array_get($user, 'user.last_name'),
        'nickname' => array_get($user, 'nickname'),
        'email' => array_get($user, 'email'),
        'gender' => array_get($user, 'user.gender')]
    );

the only values returned are "token", "id" and "email", all other values are empty if I see the query generation

Does anyone know why they are empty? Thank you

Upvotes: 1

Views: 311

Answers (1)

Thomas Kim
Thomas Kim

Reputation: 15951

They are empty because you have an instance an object, not an array. You should use the -> operator to access the properties like this:

DB::table('users')->insert(
    ['user_id' => $user->id,
    'name' => $user->name,
    'first_name' => $user->user['first_name'],
    'last_name' => $user->user['last_name'],
    'nickname' => $user->nickname,
    'email' => $user->email,
    'gender' => $user->user['gender']]
);

Upvotes: 1

Related Questions