Problem with Model::findOrNew()



  • A mistake is made:

    Undefined property: Illuminate\Database\Eloquent\Collection:$vk

    Code area:

    $vk   = Socialite::driver('vkontakte')->user();
    $user = User::findOrNew(['vk' => $vk->id]);
    

    if ($user) {
    Auth::attempt(['vk' => $user->vk], 1);

    redirect()->route('home');
    

    } else {
    $user->name = $vk->name;
    $user->vk = $vk->id;
    $user->email = $vk->email;
    $user->save();

    Auth::attempt(['vk' => $user->vk], 1);
    
    redirect()->route('home');
    

    }

    I guess it's a mistake because the table is completely empty, but how is it to be?



  • Wrong choice of method.

    Changed.

    User::firstOrNew(['vk' => $vk->id])
    



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2