如何在AuthLaravel中添加数据到相关的table?
How to add data to related table in Auth Laravel?
我在Laravel 5.3中使用标准机制认证是通过命令构建的:php artisan make:auth
.
因此,它使用以下方法创建了控制器 RegisterController
:
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]));
}
因此,我需要扩展此方法以将数据添加到用户 table 的相关 table。为此,我这样做:
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
])->roles()->attach(Role::where('name', "admin"))->first());
}
在此之后我得到错误:
FatalThrowableError in SessionGuard.php line 441:
Type error: Argument 1 passed to Illuminate\Auth\SessionGuard::login() must implement interface Illuminate\Contracts\Auth\Authenticatable, null given, called in \vendor\laravel\framework\src\Illuminate\Foundation\Auth\RegistersUsers.php on line 32
我没有使用 Laravel 的授权逻辑,但看起来用户在创建后立即登录。要在 Laravel 中登录用户需要用户对象,因此:
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
create()
方法 returns Model
. You, however, added attach()
to the return statement, and if you take a look at the API, the method returns void
最终转化为错误消息中提到的 null
。
你想做的,是这样的:
protected function create(array $data) {
// Create the User and store the object
$newUser = User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
// Add extra data
$adminRole = Role::where('name', 'like', 'admin')->first();
$newUser->roles()->attach($adminRole->id);
// Return the new User object
return $newUser;
}
注意:我假设您的 role(s)
table.
中有一个 id
列
我在Laravel 5.3中使用标准机制认证是通过命令构建的:php artisan make:auth
.
因此,它使用以下方法创建了控制器 RegisterController
:
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]));
}
因此,我需要扩展此方法以将数据添加到用户 table 的相关 table。为此,我这样做:
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
])->roles()->attach(Role::where('name', "admin"))->first());
}
在此之后我得到错误:
FatalThrowableError in SessionGuard.php line 441:
Type error: Argument 1 passed to Illuminate\Auth\SessionGuard::login() must implement interface Illuminate\Contracts\Auth\Authenticatable, null given, called in \vendor\laravel\framework\src\Illuminate\Foundation\Auth\RegistersUsers.php on line 32
我没有使用 Laravel 的授权逻辑,但看起来用户在创建后立即登录。要在 Laravel 中登录用户需要用户对象,因此:
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
create()
方法 returns Model
. You, however, added attach()
to the return statement, and if you take a look at the API, the method returns void
最终转化为错误消息中提到的 null
。
你想做的,是这样的:
protected function create(array $data) {
// Create the User and store the object
$newUser = User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
// Add extra data
$adminRole = Role::where('name', 'like', 'admin')->first();
$newUser->roles()->attach($adminRole->id);
// Return the new User object
return $newUser;
}
注意:我假设您的 role(s)
table.
id
列