Laravel 5.2 Using Associate to Update a BelongsTo Relationship
<p>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 <code>$user-&gt;profile</code> so the relationship appears to be okay in the User and UserProfile models.</p>

<p>Can anyone see what is wrong with this controller action:</p>

<pre><code>public function update(Request $request, User $user)
{
    $this-&gt;validate($request, [
        'username'     =&gt; 'required|max:32|unique:users',
        'email'        =&gt; 'required|email|max:128|unique:users',
        'first_name'   =&gt; 'required',
        'last_name'    =&gt; 'required',
        'phone_number' =&gt; 'regex:/^([0-9\s\-\+\(\)\.]*)$/',
    ]);

    $user-&gt;update($request-&gt;all());

    $profile = new UserProfile($request-&gt;all());
    // Also tried:
    //$profile = UserProfile::where(['user_id' =&gt; $user-&gt;id])-&gt;first();

    $user-&gt;profile()-&gt;associate($profile);
    $user-&gt;save();

    return response()-&gt;json([
        'message' =&gt; trans('user.updated'),
    ]);
}
</code></pre>

<p><strong>Error</strong></p>

<pre><code>BadMethodCallException in Builder.php line 2161:
Call to undefined method Illuminate\Database\Query\Builder::associate()
</code></pre>

<p><strong>User Model Relationships</strong></p>

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

<p><strong>UserProfile Model Relationship</strong></p>

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

<p><strong>Solution</strong></p>

<pre><code>$user-&gt;fill($request-&gt;all())-&gt;save();

$profile = UserProfile::where('user_id', $user-&gt;id)-&gt;first();
$profile-&gt;fill($request-&gt;all());
$user-&gt;profile()-&gt;save($profile);
</code></pre>