Laravel 5.2使用Associate更新BelongsTo关系

时间:2016-03-14 04:04:25

标签: php laravel laravel-5 laravel-5.2

我使用Route Model Binding来获取User实例,然后在验证通过后更新它,然后更新User和Profile之间的关联属于关系,但我一直收到错误。更新发生在用户上,但无法更新配置文件。根据我从文档中理解的内容,这似乎是正确的。我可以使用2016-03-07-09-27:01访问个人资料数据,因此在User和UserProfile模型中这种关系似乎没问题。

任何人都可以看到此控制器操作有什么问题:

$user->profile

错误

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'),
    ]);
}

用户模型关系

BadMethodCallException in Builder.php line 2161:
Call to undefined method Illuminate\Database\Query\Builder::associate()

UserProfile模型关系

/**
 * A user has-one profile.
 *
 * @return \Illuminate\Database\Eloquent\Relations\HasOne
 */
public function profile()
{
    return $this->hasOne('App\UserProfile');
}

解决方案

/**
 * A user profile belongs to a user.
 *
 * @return \Illuminate\Database\Eloquent\Relations\HasOne
 */
public function user()
{
    return $this->belongsTo('App\User');
}

2 个答案:

答案 0 :(得分:1)

您必须先检索或创建新的$profile实体,然后将其放入$user->profile()->save($profile);。此外,您在这里有一对一的关系,因此您应该像这样保存用户的个人资料:

The following packages have unmet dependencies: kurento-media-server-6.0 : Depends: kms-core-6.0 (>= 6.4.0) but it is not going to be installed Depends: libglib2.0-0 (>= 2.41.1) but 2.40.2-0ubuntu1 is to be installed Depends: libgstreamer1.5-0 (>= 1.7.1.1~20160224213114.199.gba35ee7.trusty) but it is not going to be installed Depends: gstreamer1.5-plugins-base (>= 1.7.0~0) but it is not going to be installed Depends: gstreamer1.5-libav (>= 1.7.0~0) but it is not going to be installed Depends: gstreamer1.5-plugins-bad (>= 1.7.0~0) but it is not going to be installed Depends: gstreamer1.5-plugins-good (>= 1.7.0~0) but it is not going to be installed Depends: gstreamer1.5-plugins-ugly (>= 1.7.0~0) but it is not going to be installed Depends: kms-elements-6.0 (>= 6.4.0) but it is not going to be installed Depends: kms-filters-6.0 (>= 6.4.0) but it is not going to be installed E: Unable to correct problems, you have held broken packages.

答案 1 :(得分:0)

将您的代码更改为:

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'),
    ]);
}