调用未定义的方法 IlluminateDatabaseQueryBuilder::associate()

2021-12-26 00:00:00 php laravel-4 eloquent

参考:如何更新Laravel 4 中现有的 Eloquent 关系?

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

我收到错误:Call to undefined method IlluminateDatabaseQueryBuilder::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);
}

推荐答案

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

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

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

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

例如

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

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

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

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.

希望这会有所帮助.

格伦

相关文章