简体   繁体   中英

How to Query Multiple Pivot Relationship in Laravel Lighthouse

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM