简体   繁体   中英

Laravel 5.4 - Create user

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.

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

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 !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM