如何在 Laravel Lighthouse 中查询多透视关系

发布于 2025-01-10 02:53:56 字数 3445 浏览 0 评论 0原文

嗨,我对 laravel-lighthouse 还很陌生。我已阅读文档,但不幸的是,没有有关此问题的详细信息。我的问题是如何查询多个数据透视表。我有一个属于许多其他模型的项目表。 代码如下。

class Item extends Model
    {
        public function provisions(): BelongsToMany
        {
            return $this->belongsToMany(Pwmp::class,
                'project_wise_material_provision_items', 'item_id', 'provision_id')
                ->withPivot('qty', 'unit_cost')
                ->withTimestamps()
                ->as('provision_items');
        }
        public function procurements(): BelongsToMany
        {
            return $this->belongsToMany(Pwpp::class,
                'project_wise_procurement_plan_items', 'item', 'procurement')
                ->withTimestamps()
                ->as('procurement_items')
                ->withPivot('qty', 'unit_cost');
        }
    
        public function log_sheets(): BelongsToMany
        {
            return $this->belongsToMany(Braols::class,
                'bid_receiving_and_opening_log_sheet_items', 'item', 'braols')
                ->withTimestamps()
                ->as('log_sheet_items')
                ->withPivot('qty', 'unit_cost', 'tax');
        }
        public function workshop(): BelongsToMany
        {
            return $this->belongsToMany(TransformerWorkshop::class,
                'transformer_workshop_items', 'item', 'workshop')
                ->as('workshop_items')
                ->withTimestamps()
                ->withPivot('qty', 'flow', 'type');
        }
        public function sub_office(): BelongsToMany
        {
            return $this->belongsToMany(MaterialReceiptInSubOffice::class,
                'material_receipt_in_sub_office_items', 'item', 'receipt')
                ->as('sub_office')
                ->withTimestamps()
                ->withPivot('id','qty', 'type');
        }

这是我的 GraphQL 架构

type Item{
        id: ID!
        workshop_items: [TransformerWorkShopItemsPivot]
        provision_items: [ProjectWiseMaterialProvisionItem]
        sub_office: MaterialReceiptInSubOfficeItemsPivot
    }
    
    type MaterialReceiptInSubOfficeItemsPivot{
        qty: Int
        type: Int
        id: ID
    }
    
    type ProjectWiseMaterialProvisionItem {
        qty: String
        unit_cost: String
    }
    type TransformerWorkShopItemsPivot{
        qty: Int
        type: Int
        flow: Int
    }
    

现在当我查询 api 时

```graphql material_receipt_in_sub_office_store(id: 4){ id items{ id provision_items{ unit_cost qty } sub_office{ type id qty } } } } ``` it returns the following result
{
  "data": {
    "material_receipt_in_sub_office_store": {
      "id": "4",
      "items": [
        {
          "id": "431",
          "name": "Drill Machine",
          "image": "crane2.jpg",
          "category_id": {
            "id": "993"
          },
          "provision_items": null,
          "sub_office": {
            "type": null,
            "id": null,
            "qty": null,
            "__typename": "MaterialReceiptInSubOfficeItemsPivot"
          }
        },

我无法获取多个数据透视表数据。 但是,如果我查询数据透视表,它只会返回一个数据透视表的数据。

Hi, there I'm fairly new to laravel-lighthouse. I've read the documentation but unfortunately, there is no detail about this issue. My question is How can I query more than one pivot table. I have an Item table that belongs to many other Models.
The code is given below.

class Item extends Model
    {
        public function provisions(): BelongsToMany
        {
            return $this->belongsToMany(Pwmp::class,
                'project_wise_material_provision_items', 'item_id', 'provision_id')
                ->withPivot('qty', 'unit_cost')
                ->withTimestamps()
                ->as('provision_items');
        }
        public function procurements(): BelongsToMany
        {
            return $this->belongsToMany(Pwpp::class,
                'project_wise_procurement_plan_items', 'item', 'procurement')
                ->withTimestamps()
                ->as('procurement_items')
                ->withPivot('qty', 'unit_cost');
        }
    
        public function log_sheets(): BelongsToMany
        {
            return $this->belongsToMany(Braols::class,
                'bid_receiving_and_opening_log_sheet_items', 'item', 'braols')
                ->withTimestamps()
                ->as('log_sheet_items')
                ->withPivot('qty', 'unit_cost', 'tax');
        }
        public function workshop(): BelongsToMany
        {
            return $this->belongsToMany(TransformerWorkshop::class,
                'transformer_workshop_items', 'item', 'workshop')
                ->as('workshop_items')
                ->withTimestamps()
                ->withPivot('qty', 'flow', 'type');
        }
        public function sub_office(): BelongsToMany
        {
            return $this->belongsToMany(MaterialReceiptInSubOffice::class,
                'material_receipt_in_sub_office_items', 'item', 'receipt')
                ->as('sub_office')
                ->withTimestamps()
                ->withPivot('id','qty', 'type');
        }

Here is my GraphQL schema

type Item{
        id: ID!
        workshop_items: [TransformerWorkShopItemsPivot]
        provision_items: [ProjectWiseMaterialProvisionItem]
        sub_office: MaterialReceiptInSubOfficeItemsPivot
    }
    
    type MaterialReceiptInSubOfficeItemsPivot{
        qty: Int
        type: Int
        id: ID
    }
    
    type ProjectWiseMaterialProvisionItem {
        qty: String
        unit_cost: String
    }
    type TransformerWorkShopItemsPivot{
        qty: Int
        type: Int
        flow: Int
    }
    

Now when i query the api

```graphql
material_receipt_in_sub_office_store(id: 4){
id
items{
id
provision_items{
unit_cost
qty
}
sub_office{
type
id
qty
}
}
}
}
```

it returns the following result

{
  "data": {
    "material_receipt_in_sub_office_store": {
      "id": "4",
      "items": [
        {
          "id": "431",
          "name": "Drill Machine",
          "image": "crane2.jpg",
          "category_id": {
            "id": "993"
          },
          "provision_items": null,
          "sub_office": {
            "type": null,
            "id": null,
            "qty": null,
            "__typename": "MaterialReceiptInSubOfficeItemsPivot"
          }
        },

I am unable to fetch the multiple pivot table data.
however, if I query pivot it returns the data of only one pivot.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小情绪 2025-01-17 02:53:56

首先,您似乎没有在类型定义中使用 Laravel 模型的关系。

通过指定 @belongsToMany / @hasMany 指令(在此处查看更多内容)您将能够优化您的查询。

type Item{
    id: ID!
    workshop_items: [TransformerWorkShopItemsPivot] @hasMany(relation: "workshop")
    provision_items: [ProjectWiseMaterialProvisionItem] @hasMany(relation: "provisions")
    sub_office: MaterialReceiptInSubOfficeItemsPivot @hasMany
}

我只是不确定您在关系定义中给出表别名的事实。

然后,通过执行查询,您可以根据需要获取尽可能多的主元关系。这样做从来没有遇到过关系问题。

First, it looks like you don't use Laravel model's relationships into your type definition.

By specifying a @belongsToMany / @hasMany directive (see more here) you'll be able to optimize your queries.

type Item{
    id: ID!
    workshop_items: [TransformerWorkShopItemsPivot] @hasMany(relation: "workshop")
    provision_items: [ProjectWiseMaterialProvisionItem] @hasMany(relation: "provisions")
    sub_office: MaterialReceiptInSubOfficeItemsPivot @hasMany
}

I'm just not sure about the fact you give table aliases in your relation definition..

Then, by doing the query, you can fetch as much pivot relations as you want. Never got relation problems by doing this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文