0

我的 laravel 项目中有经理表和资金表

经理 -> id、姓名、钱包资金 -> id、金额、fundable_type、fundable_id、应收款类型、应收款ID、状态

我在 Manager 模型中有以下关系

public function funds()
{
    return $this->morphMany(Fund::class, 'fundable');
}

public function receiveds()
{
    return $this->morphMany(Fund::class, 'receivable');
}

我在基金模型中有以下关系


public function fundable()
{
    return $this->morphTo();
}

public function receivable()
{
    return $this->morphTo();
}

$manager->funds 很好地填充了基金模型。

我想接收收到资金的经理特定经理。

我试过 $manager->funds->receivable 但它不起作用。

4

2 回答 2

0

在关系规范中,您可以附加其他模型的关系:

public function funds()
{
    return $this->morphMany(Fund::class, 'fundable')->with('receivable');
}
于 2022-01-07T02:09:40.857 回答
0

好的,我自己能够得到答案。

public function managers()
{
    return $this->hasManyThrough(Manager::class, Fund::class, 'fundable_id', 'id', 'id', 'receivable_id')
->where('fundable_type', Manager::class)->where('receivable_type', Manager::class)->distinct();
}
于 2022-01-07T14:17:54.203 回答