从嵌套数组中保存值 - Laravel

时间:2018-05-17 10:36:19

标签: php arrays laravel multidimensional-array laravel-socialite

我从API调用返回了以下数组,并希望保存“user.location.name”的值以及多维响应中包含的其他值。以下是编辑后的响应,显示结构:

Laravel\Socialite\Two\User Object
(
[token] => 
[refreshToken] => 
[expiresIn] => 
[id] => 
[nickname] => 
[name] => 
[email] => l
[avatar] => 
[user] => Array
    (
    [emailAddress] => 
    [firstName] =>
    [formattedName] =>
    [headline] =>
    [id] =>
    [industry] =>
    [lastName] =>
    [location] => Array
        (
            [country] =>
                (
                    [code] =>
                )
            [name] => *****VALUE I WANT*****
        )
    [pictureUrl] =>
    [positions] => Array
        (
            [_total] => 1
            [values] => Array
                (
                    [0] => Array
                        (
                            [company] => Array
                                (
                                    [name] => *****VALUE I WANT*****
                                )
                            [id] =>
                            [isCurrent] =>
                            [location] => Array
                                (
                                    [country] => Array
                                        (
                                            [code] =>
                                            [name] =>
                                        )
                                    [name] =>
                                )
                            [startDate] => Array
                                (
                                    [month] =>
                                    [year] =>
                                )
                            [summary] => 
                            [title] =>
                        )
                )
        )

    [publicProfileUrl] =>
    [summary] =>
[avatar_original] => 
)

然后我检查用户是否已经存在,然后使用以下函数将它们保存在数据库中:

     public function userFindOrCreate($linkedin_user)
 {
    $user = User::where('provider_id', $linkedin_user->id)->first();

    if (!$user){
      $user = new User;
      $user->name = $linkedin_user->getName();
      $user->email = $linkedin_user->getEmail();
      $user->picture = $linkedin_user->getAvatar();
      $user->provider_id = $linkedin_user->getId();
      $user->access_token = $linkedin_user->token;
      $user->linkedin = $linkedin_user->getRaw()['publicProfileUrl'];
      $user->location = ?????
      $user->company_name = ????

      $user->save();
    }
    return $user;
 }

如何在上面的代码中访问显示为****** VALUE I WANT ******的值并将其保存到新用户?点符号不适用于getRaw()方法,因此我无法使其工作(尽管它适用于1层深层的事情,例如publicProfileUrl)。

我也尝试过引用

use Illuminate\Support\Arr; 

并使用

$user->location = Arr::get($linkedin_user, 'user.location.name') 

虽然这不起作用......

0 个答案:

没有答案