Reputation: 1353
I'm integrating Facebook login in my web app, but I get this error:
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
I saw that the issue is when I create the new user:
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!
}
User is created correctly but I get the error above. I can't see my message "Hello". If I replace the create method like this:
return User::create([
'name' => $user->name,
'email' => $user->email,
'provider' => $provider,
'provider_id' => $user->id
]);
it works well. But I don't want stop after create user; I would like return another view.
Upvotes: 4
Views: 8431
Reputation: 4022
I do not think this line is causing the problem $test = DB::table('users')->inse...
check in if condition
if ($authUser) {
dd('here comes the boom.....');
return $authUser;
}
Also keep in mind $authUser
is User
type object where as $test
is boolean
!
Upvotes: 0
Reputation: 4620
There is several ways how to insert new row into table in laravel.
One way to do it is
User::create([
'name' => $user->name,
'email' => $user->email,
'provider' => $provider,
'provider_id' => $user->id
]);
return view('your_view');
But you need to have this in your model (for mass assignment)
protected $fillable = ['name','email','provider','provider_id'];
Other way to insert new row is next
$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');
Hope it helps you. For more additional help check official documentation
Upvotes: 2