Laravel 5.2 使用 Associate 更新 BelongsTo 关系
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35979362/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel 5.2 Using Associate to Update a BelongsTo Relationship
提问by mtpultz
I'm using Route Model Binding to get a User instance then update it if validation passes, and then update the associated belongs to relationship between User and Profile, but I keep getting an error. The update occurs on the User, but fails on updating the Profile. From what I've understood from the docs this appears to be correct. I can access Profile data using $user->profile
so the relationship appears to be okay in the User and UserProfile models.
我正在使用路由模型绑定来获取用户实例,然后在验证通过时更新它,然后更新关联的属于用户和配置文件之间的关系,但我不断收到错误消息。更新发生在用户上,但在更新配置文件时失败。从我从文档中了解到的,这似乎是正确的。我可以使用访问 Profile 数据,$user->profile
因此关系在 User 和 UserProfile 模型中似乎没问题。
Can anyone see what is wrong with this controller action:
任何人都可以看到此控制器操作有什么问题:
public function update(Request $request, User $user)
{
$this->validate($request, [
'username' => 'required|max:32|unique:users',
'email' => 'required|email|max:128|unique:users',
'first_name' => 'required',
'last_name' => 'required',
'phone_number' => 'regex:/^([0-9\s\-\+\(\)\.]*)$/',
]);
$user->update($request->all());
$profile = new UserProfile($request->all());
// Also tried:
//$profile = UserProfile::where(['user_id' => $user->id])->first();
$user->profile()->associate($profile);
$user->save();
return response()->json([
'message' => trans('user.updated'),
]);
}
Error
错误
BadMethodCallException in Builder.php line 2161:
Call to undefined method Illuminate\Database\Query\Builder::associate()
User Model Relationships
用户模型关系
/**
* A user has-one profile.
*
* @return \Illuminate\Database\Eloquent\Relations\HasOne
*/
public function profile()
{
return $this->hasOne('App\UserProfile');
}
UserProfile Model Relationship
用户配置文件模型关系
/**
* A user profile belongs to a user.
*
* @return \Illuminate\Database\Eloquent\Relations\HasOne
*/
public function user()
{
return $this->belongsTo('App\User');
}
Solution
解决方案
$user->fill($request->all())->save();
$profile = UserProfile::where('user_id', $user->id)->first();
$profile->fill($request->all());
$user->profile()->save($profile);
采纳答案by Alexey Mezenin
You must retrieve or create new profile
entity first and put it in $profile
. Also, you have One-to-one relation here, so you should save your user's profile like this:
您必须首先检索或创建新profile
实体并将其放入$profile
. 此外,您在这里有一对一的关系,因此您应该像这样保存用户的个人资料:
$user->profile()->save($profile);
$user->profile()->save($profile);
回答by Jilson Thomas
Change your code to this:
将您的代码更改为:
public function update(Request $request, User $user)
{
$this->validate($request, [
'username' => 'required|max:32|unique:users',
'email' => 'required|email|max:128|unique:users',
'first_name' => 'required',
'last_name' => 'required',
'phone_number' => 'regex:/^([0-9\s\-\+\(\)\.]*)$/',
]);
$profile = UserProfile::create($request->all());
$user->profile()->associate($profile);
$user->save();
return response()->json([
'message' => trans('user.updated'),
]);
}