调用未定义的方法 Illuminate\Database\Query\Builder::associate()

2022-08-30 17:48:45

参考:如何更新《拉拉维尔4》中现有的雄辩关系?

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

我收到错误:Call to undefined method Illuminate\Database\Query\Builder::associate()

这是整个方法:

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);
}

答案 1

associate() 是 belongsTo 关系的一种方法,但从上面看,你试图通过 hasOne 关系来调用它。

我只是猜测,因为你没有提供你雄辩的模型类代码,所以看不到你如何准确地设置关系,但如果你有:

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

class Userinfo extends Eloquent {

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

然后需要对 Userinfo 调用关联,因为这具有 association() 方法附加到的 belongsTo 关系。

例如

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

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

将user_info表中的外键user_id设置为$user对象的 id。

看看你上面的代码,似乎这不是你真正想要做的事情,而且

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

您注释掉的调用实际上将执行您似乎正在尝试实现的目标,即更新与当前用户相关的用户信息(如果该用户已存在)。

希望这有帮助。

幽谷


答案 2

更改为 。它看起来像这样:hasOnebelongsTo

class User extends Eloquent {

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

class Userinfo extends Eloquent {

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

推荐