【问题标题】:How to get pivot data in laravel lighthouse?如何在 laravel 灯塔中获取枢轴数据?
【发布时间】:2021-10-02 11:03:45
【问题描述】:

我使用 lighthouse 包来制作我的 graphql 架构 (https://github.com/nuwave/lighthouse)

但是如何从存储在数据透视表中的@belongsToMany 关系中获取数据?

例如Document 实体 N-N 连接到 Person 实体。每个 Person 在数据透视表中都有一些元数据。

foreign_citizens: [Person!] @belongsToMany(relation: "foreignCitizens"),

【问题讨论】:

    标签: laravel graphql laravel-lighthouse


    【解决方案1】:

    首先,您需要在关系末尾使用withPivot 在 laravel 中获取您的数据透视数据,如下所示:

    public function foreignCitizens(): BelongsToMany
    {
      return $this->belongsToMany()->withPivot(/* here is a list of your pivot data */);
    }
    

    那么你需要在graphql中定义合适的type

    type ForeignCitizenPivot {
      # Here goes the same list again
    }
    

    然后在你的ForeignCitizen 中添加一个pivot 再次输入graphql:

    type ForeignCitizen {
      # other attributes
      pivot: ForeignCitizenPivot 
    }
    

    【讨论】:

      猜你喜欢
      • 2020-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-27
      • 2021-03-31
      相关资源
      最近更新 更多