我想使用雄辩模型更新数据库中的记录。我在更新表时出错user_profile
说where id is not null
.
用户模型
class User extends Model
{
public function profile()
{
return $this->hasOne('AppModelsCommonUserProfile');
}
}
用户配置文件模型
class UserProfile extends Model
{
public function user()
{
return $this->belongsTo('AppModelsUser');
}
}
控制器
$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';
在模型类中。