Call to undefined method Illuminate\Database\Query\Builder::associate()

73,370

Solution 1

associate() is a method of the belongsTo relationship, but it looks like from the above you are trying to call it via the hasOne relationship.

I am just guessing as you have not provided your eloquent model class code so can't see how you have set the relationships exactly, but if you have:

class User extends Eloquent {
    public function userinfo()
    {
        return $this->hasOne('Userinfo');
    }
}

class Userinfo extends Eloquent {

    public function user() {
        return $this->belongsTo('User');
    }
}

Then associate needs to be called against Userinfo as this has the belongsTo relationship to which the associate() method is attached.

For example

$user = \User::find(4);      
$userinfo = \UserInfo::find(1);

$userinfo->user()->associate($user);
$userinfo->save();

Will set the foreign key user_id in the user_info table to the id of the $user object.

Looking at your above code it doesn't appear that this is what you are actually trying to do and that the

$user->userinfo()->update($userinfoArray);

call which you have commented out will in fact do what you seem to be trying to achieve, which is to update the userinfo that is related to the current user if that user already exists.

Hope this helps.

Glen

Solution 2

Change hasOne to belongsTo. It will look like:

class User extends Eloquent {

    public function userinfo()
    {
        return $this->belongsTo('Userinfo');
    }
}

class Userinfo extends Eloquent {

    public function user() {
        return $this->belongsTo('User');
    }
}
Share:
73,370
drack
Author by

drack

Updated on August 07, 2022

Comments

  • drack
    drack over 1 year

    Reference: How can I update an existing Eloquent relationship in Laravel 4?

    $userinfo = \Userinfo::find($id);
    \User::find($id)->userinfo()->associate($userinfo)->save();
    

    I'm getting the error: Call to undefined method Illuminate\Database\Query\Builder::associate()

    Here is the entire method:

    public function saveUser($id)
    {
        $user = \User::find($id);
    
        $userdata = \Input::all();
    
        $rules = array(
            'email' => 'required|email',
            'state' => 'size:2',
            'zip'   => 'size:5',
            'phone' => array('regex:/^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/')
        );
    
        $validator = \Validator::make($userdata, $rules);
    
        if ($validator->passes())
        {
            if ($userdata['email'] !== $user->email)
            {
                $rules = array('email' => 'unique:users');
                $validator = \Validator::make($userdata, $rules);
                if ($validator->fails()) return Redirect::route('admin.user.edit', array('user' => $user))
                    ->with('error', 'Specified email already exists.');
            }
    
            $user->email                = $userdata['email'];
            $user->firstname            = $userdata['firstname'];
            $user->lastname             = $userdata['lastname'];
    
            $userinfoArray = array(
                'address'   => $userdata['address'],
                'city'      => $userdata['city'],
                'state'     => $userdata['state'],
                'zip'       => $userdata['zip'],
                'phone'     => preg_replace('/[^0-9]/', '', $userdata['phone'])
            );
    
            $user->save();
    
            if (!$user->userinfo)
            {
                $userinfo = new \Userinfo($userinfoArray);
                $userinfo = $user->userinfo()->save($userinfo);
            }
            else
            {
                $userinfo = \Userinfo::find($id);
                \User::find($id)->userinfo()->associate($userinfo)->save();
                //$user->userinfo()->update($userinfoArray);
            }
    
            return \Redirect::route('admin.user.detail', array('id' => $id))
                ->with('success', 'User updated.');
        }
    
        return \Redirect::route('admin.user.edit', array('id' => $id))
            ->withInput()
            ->withErrors($validator);
    }