如何解决如何在laravel灯塔中获取枢轴数据?
我使用灯塔包来制作我的 graphql 模式 (https://github.com/nuwave/lighthouse)
但是如何从存储在数据透视表中的 @belongsToMany
关系中获取数据?
例如Document
实体将 N-N 连接到 Person
实体。每个 Person 在数据透视表中都有一些元数据。
foreign_citizens: [Person!] @belongsToMany(relation: "foreignCitizens"),
解决方法
首先,您需要在关系末尾使用 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
}
然后再次将 pivot
添加到您在 graphql 中的 ForeignCitizen
类型:
type ForeignCitizen {
# other attributes
pivot: ForeignCitizenPivot
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。