Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Laravel 拉威尔模型公司;关系_Laravel_Eloquent_Models_Relationships - Fatal编程技术网

Laravel 拉威尔模型公司;关系

Laravel 拉威尔模型公司;关系,laravel,eloquent,models,relationships,Laravel,Eloquent,Models,Relationships,我试图在用户和债券之间建立一种关系。一个债券总是包含两个用户(债务人、债权人) 最后,如果我调用$bond->debtor或$bond->bender 用户模型: public function bonds() { return $this->hasMany(Bond::class); } 债券模型: protected $fillable = ['description', 'amount', 'debtor_id', 'creditor_id']; public funct

我试图在用户和债券之间建立一种关系。一个债券总是包含两个用户(债务人、债权人)

最后,如果我调用
$bond->debtor
$bond->bender

用户模型:

public function bonds()
{
    return $this->hasMany(Bond::class);
}
债券模型:

protected $fillable = ['description', 'amount', 'debtor_id', 'creditor_id'];

public function debtor()
{
    return $this->belongsToMany(User::class, 'bond_user', 'debtor_id', 'user_id');
}

public function creditor()
{
    return $this->belongsToMany(User::class, 'bond_user', 'creditor_id', 'user_id');
}
public function debtor()
{
    return $this->belongsTo(User::class, 'debtor_id');
}

public function creditor()
{
    return $this->belongsTo(User::class, 'creditor_id');
}
和两次迁移:

Schema::create('bonds', function (Blueprint $table) {
        $table->increments('id');
        $table->text('description');
        $table->float('amount', 8, 2);
        $table->integer('debtor_id');
        $table->integer('creditor_id');
        $table->timestamps();
    });

Schema::create('bond_user', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('user_id');
        $table->integer('debtor_id');
        $table->integer('creditor_id');
        $table->timestamps();
    });
每当我调用例如,
$bond->debtor
时,我都会得到一个整数,而不是用户模型


更新

我找到了解决办法:

用户模型:

public function bond_debtor()
{
    return $this->hasMany(Bond::class, 'debtor_id');
}

public function bond_creditor()
{
    return $this->hasMany(Bond::class, 'creditor_id');
}
债券模型:

protected $fillable = ['description', 'amount', 'debtor_id', 'creditor_id'];

public function debtor()
{
    return $this->belongsToMany(User::class, 'bond_user', 'debtor_id', 'user_id');
}

public function creditor()
{
    return $this->belongsToMany(User::class, 'bond_user', 'creditor_id', 'user_id');
}
public function debtor()
{
    return $this->belongsTo(User::class, 'debtor_id');
}

public function creditor()
{
    return $this->belongsTo(User::class, 'creditor_id');
}

在设置关系时,您需要

  • 将引用列的字段类型设置为
    integer
    unsigned

    $table->integer('debtor_id')->unsigned();
    $table->integer('creditor_id')->unsigned();
    
  • 设置与
    外部
    方法的关系:

    $table->foreign('debtor_id')
        ->references('id')->on('user');
    $table->foreign('creditor_id')
        ->references('id')->on('user');
    

  • 你试过$bond->debtor()而不是$bond->debtor()吗?你觉得它们会产生不同的结果。我不需要你的建议就可以解决这个问题。“这些是建立这种关系的最佳实践吗?”塞巴斯蒂安猜测是的,这是我学习的方式,直到现在都没有问题。