Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Eager Loading not Working for BelongsToMany Pivot Relation #15

Open
nouman-ashraf-awan opened this issue Nov 26, 2024 · 1 comment
Open

Comments

@nouman-ashraf-awan
Copy link

Laravel Version: 9.0
Nova Version: 3.0
PHP Version: 8.1
Description:
BelongsToMany relationships with custom pivot fields are not eager loading nested relation in the Nova resource.

Detailed steps to reproduce the issue on a fresh Nova installation:
Create a Group model with a currencies relation:

public function currencies(): BelongsToMany
{
return $this->belongsToMany(
Currency::class,
CurrencyGroup::class,
'group_id',
'currency_id'
)
->withPivot('id', 'amount', 'remaining', 'type', 'begins_at', 'expires_at', 'contract_id')
->withTimestamps();
}
In Nova Resource
BelongsToMany::make('Currencies Breakdown', 'currencies', Currency::class)
->fields(new CurrencyGroupFields)
->allowDuplicateRelations()
->canSee(fn () => Setting::get('ff_enable_currency_system')),

In CurrencyGroup Model
public function currencyUsers()
{
return $this->hasMany(CurrencyUser::class, 'currency_group_id', 'id');
}

Now wants to eager load currencies.pivot.currencyUsers or some other relations in Group Resource but not working. Creating N+1 Problem.
While using detailQuery method, eager loading working for nova-api/groups/ID
but not in
/nova-api/currencies?orderBy=&page=1&perPage=5&relationshipType=belongsToMany&search=&trashed=&viaRelationship=currencies&viaResource=groups&viaResourceId=ID

Tried this package https://github.com/ajcastro/eager-load-pivot-relations too but not working in Nova. Its working in Model but not in Nova.

@nouman-ashraf-awan
Copy link
Author

@ajcastro

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant