laravel ...在创建用户时插入默认配置文件

时间:2014-11-22 18:33:05

标签: php laravel

我有UserProfile模型,关系为1:1。当用户注册到我的网站时,我想创建一个包含与该用户相关联的defaultProfile数据的个人资料记录。目前我正在使用这种方法,它的工作非常好:

$newUser = $this->user->create($data);
$profile= new Profile($this->defaultProfile);
$newUser->profile()->save($profile);

但是我想知道,有没有办法没有在我的代码中实例化甚至注入 Profile模型?只需了解用户模型hasOne配置文件的事实。我试过这段代码:

$newUser = $this->user->create($data);
$profile= $this->user->profile()->create($this->defaultProfile);

但是,它尝试构建user_id为空的查询。我也尝试过硬编码" user_id"在我的defaultProfile数组中但是它给了我相同的错误消息:

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'user_id' cannot be null (SQL: insert into `profiles` (`about`, `user_id`, `updated_at`, `created_at`) values (foo, , 2014-11-22 18:11:49, 2014-11-22 18:11:49))

0 个答案:

没有答案
相关问题