gpt4 book ai didi

php - Laravel 保存关系

转载 作者:行者123 更新时间:2023-12-01 22:35:37 24 4
gpt4 key购买 nike

如何保存新用户关系?

用户模型:

public function profile(){
return $this->hasOne('Profile','id');
}

个人资料模型:

protected $table = 'users_personal';
public function user(){
return $this->belongsTo('User','id');
}

主要功能:

            $u                      = new User;
$u->username = $i['username'];
$u->email = $i['mail'];
$u->password = Hash::make( $i['password'] );
$u->type = 0;
$u->profile->id = $u->id;
$u->profile->name = $i['name'];
$u->profile->surname = $i['surname'];
$u->profile->address = $i['address'];
$u->profile->number = $i['strnum'];
$u->profile->city = $i['city'];
$u->profile->ptt = $i['ptt'];
$u->profile->mobile = $i['mobile'];
$u->profile->birthday = $i['year'].'-'.$i['mob'].'-'.$i['dob'];
$u->profile->newsletter = $i['news'];
$u->push();

如果我这样做,我会得到一个错误:间接修改重载属性 User::$profile 没有效果

创建新用户时如何保存用户配置文件?

最佳答案

您应该创建您的 Profile 对象,然后将其附加到您的用户。

$u                      = new User();
$u->username = $i['username'];
$u->email = $i['mail'];
$u->password = Hash::make( $i['password'] );
$u->type = 0;
$u->save();

$profile = new Profile();
$profile->id = $u->id;
$profile->name = $i['name'];
$profile->surname = $i['surname'];
$profile->address = $i['address'];
$profile->number = $i['strnum'];
$profile->city = $i['city'];
$profile->ptt = $i['ptt'];
$profile->mobile = $i['mobile'];
$profile->birthday = $i['year'].'-'.$i['mob'].'-'.$i['dob'];
$profile->newsletter = $i['news'];

$u->profile()->save($profile);

关于php - Laravel 保存关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24001109/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com