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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39993787/
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-09-14 14:35:51  来源:igfitidea点击:

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

phplaravellaravel-5laravel-5.2

提问by Jay

I am using laravel 5.2 and I am getting this error while creating user.

我正在使用 laravel 5.2,在创建用户时出现此错误。

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

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

this is my User.php

这是我的 User.php

namespace App;

use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{

protected $fillable = [
    'name', 'email', 'password', 'role_id'
];

protected $hidden = [
    'password', 'remember_token',
];

public function role()
{
    return $this->hasOne('App\Role');
}
}

my role.php

我的角色.php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Role extends Model
{
protected $table = "roles";

protected $fillable = [
    'name','description'
];

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

and this is migration I used

这是我使用的迁移

public function up()
{
    Schema::create('roles', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->string('description');
    });

    Schema::create('users', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->string('email')->unique();
        $table->string('password');
        $table->integer('role_id')->unsigned();
        $table->foreign('role_id')->references('id')->on('roles');
        $table->rememberToken();
        $table->timestamps();
    });
}

this is controller code I am using

这是我正在使用的控制器代码

$role = Role::find(1);

    $user = new User();
    $user->name = "Admin";
    $user->email = "[email protected]";
    $user->password = bcrypt("password");
    $user->role()->associate($role);
    $user->save();

when I run this code I get "Call to undefined method Illuminate\Database\Query\Builder::associate()" error

当我运行此代码时,我收到“Call to undefined method Illuminate\Database\Query\Builder::associate()”错误

let me know what is wrong with my code.

让我知道我的代码有什么问题。

回答by Neat

The associate()function is used to update a belongsTo()relationship. Your role()relationship is a hasOne(), thats why the method doesn't exist.

associate()函数用于更新belongsTo()关系。您的role()关系是hasOne(),这就是该方法不存在的原因。

Source

来源

回答by AddWeb Solution Pvt Ltd

I think you can use associate()method to associate role then change your relationship like below:

我认为您可以使用associate()方法来关联角色,然后更改您的关系,如下所示:

return $this->hasOne('App\Role');

replace with

用。。。来代替

return $this->belongsTo('App\Role');

and

return $this->belongsTo('App\User');

replace with

用。。。来代替

return $this->hasOne('App\User');

Hope this help you well!

希望这对你有帮助!