![](/img/trans.png)
[英]how to automatically update one table column by another table column update in laravel
[英]how to update value in one column and automaticaly updated another column at different table laravel
我嘗試更新表學生中的名稱值並自動更新表用戶中的列名
我的 controller 代碼是
public function update(Request $request, $id)
{
$user = User::where('id', $id)->first();
$user->name = $request->name;
$user->save();
$student= Student::where('id', $id)->first();
$student->name = $request->name;
$student->save();
return redirect()->route('profilestudent.edit', Auth::user()->student->id);
}
沒有錯誤,但它在表用戶處更改了另一個用戶的名稱。
您可以嘗試使用mutators :
class User extends Model
{
public function setNameAttribute($value)
{
$student= Student::where('user_id', $this->id)->first() if id is not key in Student
$student->name = $value;
$student->save();
$this->attributes['first_name'] = $value;
}
}
你應該使用觀察者。 更新用戶時,學生也將自動更新。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.