2017-02-11 72 views
2

我整合Facebook登錄我的web應用程序,但我得到這個錯誤:Laravel 5.4 - 創建用戶

ErrorException in SessionGuard.php line 407: Argument 1 passed to Illuminate\Auth\SessionGuard::login() must implement interface Illuminate\Contracts\Auth\Authenticatable, string given, called in /Applications/XAMPP/xamppfiles/htdocs/shop/vendor/laravel/framework/src/Illuminate/Auth/AuthManager.php on line 294 and defined

我看到的問題是,當我創建新用戶:

public function findOrCreateUser($user, $provider) 
{ 
    $authUser = User::where('provider_id', $user->id)->first(); 
    // if user exist login 
    if ($authUser) { 
     return $authUser; 
    } 

    // if user don't exist - Create new user 
    $test = DB::table('users')->insert([ 
       'name'  => $user->name, 
       'email' => $user->email, 
       'provider' => $provider, 
       'provider_id' => $user->id 
      ]); 

    return 'Hello'; // just try if it work! 
} 

用戶創建正確,但我得到上述錯誤。我看不到我的消息「你好」。如果我更換這樣的創建方法:

return User::create([ 
     'name'  => $user->name, 
     'email' => $user->email, 
     'provider' => $provider, 
     'provider_id' => $user->id 
]); 

它運行良好。但是我不想在創建用戶後停止;我想回到另一種觀點。

回答

0

如何在laravel中將新行插入到表中有幾種方法。

一種方式來做到這一點是

User::create([ 
     'name'  => $user->name, 
     'email' => $user->email, 
     'provider' => $provider, 
     'provider_id' => $user->id 
]); 
return view('your_view'); 

但是你需要有這個模型中的(大規模分配)

protected $fillable = ['name','email','provider','provider_id']; 

插入新行另一種方式是未來

$user_database = new User; 
$user_database->name = $user->name; 
$user_database->email = $user->email; 
$user_database->provider = $provider; 
$user_database->provider_id = $user->id; 
$user_database->save(); 
return view('your_view'); 

希望它可以幫助你。欲瞭解更多其他幫助檢查official documentation

+0

我得到相同的錯誤:( –

0

我不認爲這條線是造成問題$test = DB::table('users')->inse...檢查中如果條件

if ($authUser) { 
    dd('here comes the boom.....'); 
    return $authUser; 
} 

還要記住$authUserUser類型的對象,其中如$testboolean