繁体   English   中英

一对一关系的口才更新

[英]Eloquent Update with One-to-One relationship

我想使用Eloquent模型更新数据库中的记录。 我在更新user_profile表时出错,说where id is not null

用户模型

class User extends Model
{
    public function profile()
    {
        return $this->hasOne('App\Models\Common\UserProfile');
    }
}

UserProfile模型

class UserProfile extends Model
{
    public function user()
    {
        return $this->belongsTo('\App\Models\User');
    }
}

调节器

$user->fill($data);
$user->save();
$user->profile->fill($data);
$user->profile->save();

在MySQL查询中,它看起来像这样:

UPDATE users, user_profiles
SET users.name='Test Name',
user_profiles.email='test@gmail.com'
WHERE users.id=1
 AND user_profiles.user_id=1;

我知道MySQL查询,但是我是Eloquent新手,我想在Eloquent model 也许这里有些可以解释魔术在《 Eloquent工作原理。

默认情况下,雄辩地假设任何模型都有一个id列作为主键。 如果不是这种情况,则需要使用以下命令“注册”主键

protected $primaryKey = 'user_id';

在模型类中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM