在Laravel 5.4中,他们对用户身份验证系统进行了硬编码,所以当您使用手工命令' make :auth‘时,所有内容都将在幕后为您创建,但问题是,当我的用户成功注册时,我希望创建一个新的“profile类”实例,并使表列为空,直到用户填写他的配置文件,那么我可以在哪里放置创建用户配置文件的代码呢?
发布于 2017-05-16 12:45:36
在RegisterController中,您可以重写registered函数。
此函数在用户成功注册后直接调用。
/**
* The user has been registered.
*
* @param \Illuminate\Http\Request $request
* @param mixed $user
* @return mixed
*/
protected function registered(Request $request, $user)
{
// Create a profile here
}或者,您也可以直接对用户模型上的模型事件执行此操作。
class User extends Authenticatable
{
protected static function boot()
{
parent::boot();
static::creating(function($user) {
// Create profile here
});
}
}发布于 2017-05-16 13:09:23
在创建新用户之后,在app\Http\Controllers\Auth\RegisterController.php上的create()方法中,您可以这样做:
use App\Profile; // <-- Import this at the top
protected function create(array $data)
{
$user = User::create([ // <-- change this as you see fit
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
Profile::create(['user_id' => $user->id]);
return $user;
}https://stackoverflow.com/questions/44001776
复制相似问题