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

后端 未结 4 833
被撕碎了的回忆
被撕碎了的回忆 2021-01-01 11:29

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

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


        
相关标签:
4条回答
  • 2021-01-01 12:11

    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.

    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.

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

    fix:

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

    Hope this helps!

    0 讨论(0)
  • 2021-01-01 12:18

    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

    0 讨论(0)
  • 2021-01-01 12:18

    You need to specify the field related like this:

        public function profile()
        {
            return $this->hasOne('App\AdmProfile', 'id');
        }
    
    0 讨论(0)
  • 2021-01-01 12:23

    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');
        }
    }
    
    0 讨论(0)
提交回复
热议问题