zabarah
5 months ago
732
3
Laravel

Socialite/Facebook adding to Database

Posted 5 months ago by zabarah

Hey All,

I am still learning laravel and decided to dive head first into building a project. I do have a fair bit of knowledge but I am stuck on this aspect.

I used socialite to login a user using Facebook and modified it a bit to include a bit more information such as 'birthday', 'ad accounts' and 'facebook pages'.

The ad accounts returns an array within an array:

Laravel\Socialite\Two\User {#296 ▼ +token: "REDACTED ▶" +refreshToken: null +expiresIn: null +id: "2342342325763" +nickname: null +name: "Zabarah" +email: "[email protected]" +avatar: "https://graph.facebook.com/v5.0/82342342325763/picture?type=normal" +user: array:7 [▶] +"birthday": "04/20/1969" +"accounts": array:2 [▶] +"adaccounts": array:2 [▼ "data" => array:9 [▼ 0 => array:2 [▶] 1 => array:2 [▶] 2 => array:2 [▶] 3 => array:2 [▶] 4 => array:2 [▶] 5 => array:2 [▶] 6 => array:2 [▶] 7 => array:2 [▶] 8 => array:2 [▼ "name" => "ad account#2" "id" => "act_517861234234935" ] ] "paging" => array:1 [▶] ] +"avatar_original": "https://graph.facebook.com/v5.0/8623423425763/picture?width=1920" +"profileUrl": null }

and this is my controller to save it

public function fbCallBack(){ $user = Socialite::driver('facebook')->fields(self::fbFields)->user();

    if ($user){
        $newUser = new User();
        $newUser->name = $user->getName();
        $newUser->email = $user->getEmail() == '' ? '' : $user->getEmail();
        $newUser->password = '';

        // I setup a relationship between Adaccount model and User model
        $newUser->adAccounts()= $user->getAdAccounts();

        $newUser->save();
        $newUser = $user;
    }
    return redirect('/');
}

So my question is, how do i save the (array within the array) ad account name and id into the DB?

Please sign in or create an account to participate in this conversation.