我已经在互联网上看到了与此相关的信息,但是我似乎不太了解它,或者答案很旧并且与laravel的较旧版本有关,所以有人可以向我解释如何做到这一点吗? / p>
所以可以说我有一个users
的主模型,而我的用户模型则具有多个关系
public function contacts(){
return $this->hasMany('App\Models\Users\UserContacts', 'user_id', 'id');
}
public function discounts(){
return $this->hasMany('App\Models\Users\UserDiscounts','user_id', 'id');
}
然后在更新功能中,我正在更新用户和这种关系
//Get the user to update
$user = User::findOrFail($id);
$user->name = $request->name;
$user->age = $request->age;
$user->discounts->amount = $request->discount_amount;
$user->discounts->start_date = $request->discount_start_date;
$user->contacts->name = $request->contact_name;
$user->contacts->email = $request->contact_email;
如何保存它,以便保存用户模型和所有关系?
$user->save(); //Only works for user model
$user->update(); //Only works for user model
$user->discounts->save(); //Only works for discount model
我有一个很大的关系列表,理想情况下,我希望能够用1条命令将它们全部保存。这可能吗?
谢谢