Laravel 5.4 - 创建用户
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42176473/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel 5.4 - Create user
提问by Diego Cespedes
I'm integrating Facebook login in my web app, but I get this error:
我正在我的网络应用程序中集成 Facebook 登录,但出现此错误:
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
SessionGuard.php 第 407 行中的 ErrorException:传递给 Illuminate\Auth\SessionGuard::login() 的参数 1 必须实现接口 Illuminate\Contracts\Auth\Authenticatable,给出的字符串,在 /Applications/XAMPP/xamppfiles/htdocs/shop/vendor 中调用/laravel/framework/src/Illuminate/Auth/AuthManager.php 在第 294 行并定义
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:
用户已正确创建,但出现上述错误。我看不到我的消息“你好”。如果我像这样替换 create 方法:
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.
它运作良好。但我不想在创建用户后停止;我想返回另一个视图。
回答by KuKeC
There is several ways how to insert new row into table in laravel.
有几种方法可以在 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
希望对你有帮助。如需更多额外帮助,请查看官方文档
回答by Atiqur
I do not think this line is causing the problem $test = DB::table('users')->inse...
check in if condition
我不认为这条线是导致问题$test = DB::table('users')->inse...
检查 if 条件
if ($authUser) {
dd('here comes the boom.....');
return $authUser;
}
Also keep in mind $authUser
is User
type object where as $test
is boolean
!
也请记住$authUser
是User
类型对象,其中为$test
是boolean
!