【问题标题】:hasOneThrough relationship LaravelhasOneThrough 关系 Laravel
【发布时间】:2023-02-04 19:27:38
【问题描述】:

合同有代表(即用户)。

目前我在我的 Contract 模型中直接与 belongsTo 建立了这种关系:

class Contract extends Model
{
    public function representative()
    {
        return $this->belongsTo(User::class);
    }

我希望能够通过 organization_user 表检索此关系并检索“位置”属性。 我想我应该使用 hasOneThrough 关系,但我找不到我要找的东西。

class Contract extends Model
{
    public function representative()
    {
        return $this->hasOneThrough(User::class, OrganizationUser::class);
    }

【问题讨论】:

    标签: laravel eloquent


    【解决方案1】:

    不确定您是否能够更改数据库结构,但在我看来,您的表 contracts 和 organization_user 正在相互复制,存储相同的数据。

    仅保留一个表 organization_user 作为具有该位置的数据透视表是否有意义?

    那么你就不需要对几个关系进行大循环来获取所需的数据。

    【讨论】:

      猜你喜欢
      • 2020-10-24
      • 1970-01-01
      • 2020-10-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-03
      相关资源
      最近更新 更多