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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19868838/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 20:09:25  来源:igfitidea点击:

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

phplaravel-4eloquent

提问by drack

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

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

$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()

我收到错误: 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);
}

回答by glendaviesnz

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.

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

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.

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

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.

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

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

格伦

回答by Boston Kenne

Change hasOneto belongsTo. It will look like:

更改hasOnebelongsTo。它看起来像:

class User extends Eloquent {

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

class Userinfo extends Eloquent {

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

回答by LB_Donegal

I was stuck on this problem for a few days and it ended up being quite simple to solve. I had created a folder called 'models' in my 'app' folder but I had forgotten to reconfigure my auth.php file.

我被这个问题困了几天,结果解决起来很简单。我在“app”文件夹中创建了一个名为“models”的文件夹,但我忘记重新配置我的 auth.php 文件。

This was my error.

这是我的错误。

Call to undefined method Illuminate\Database\Query\Builder

I fixed it by opening the auth.php in the config folder and changing the following line to include my models folder.

我通过打开 config 文件夹中的 auth.php 并更改以下行以包含我的模型文件夹来修复它。

'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => Foodie\User::class,

fix:

使固定:

'providers' => [
    'users' => [
        'driver' => 'eloquent',
        'model' => Foodie\Models\User::class,

Hope this helps!

希望这可以帮助!

回答by Fischer Tirado

You need to specify the field related like this:

您需要像这样指定相关的字段:

    public function profile()
    {
        return $this->hasOne('App\AdmProfile', 'id');
    }