通过引用外键更新

Update by referring foreign key

密码已存在于 Passw Model/Table 中。在这里我想更改我的密码。

我想使用 regist_id(外键)更新,而不是 table 密码中的 ID。

控制器:

$passw = Passw::whereRegist_id($id)->get();       //Regist_id is an foregin key
$regist->pass()->update([
                    'password1' => $request->newpassword,
                    'password2' => $request->newpassword1
                ]);
return view('welcome');

注册型号:

public function pass(){
    return $this->hasOne('App\Passw');
}

当我提供更新时,它会重定向到欢迎页面。但没有更改密码值。

我哪里弄错了

你可以这样更新

$passw = Passw::where('regist_id', $id)->first();

$passw->update([
                 'password1' => $request->newpassword,
                 'password2' => $request->newpassword1
              ]);

您可以使用 $id

获取 Regist 模型
$regist = Regist::with('pass')->find($id);

$regist->pass->update([
         'password1' => $request->newpassword,
         'password2' => $request->newpassword1
]);