Skip to content

Some query methods are not callable when using the onQuery hook in Lucid relationships #1482

Answered by thetutlage
Obapelumi asked this question in Help
Discussion options

You must be logged in to vote

Hey @Obapelumi. Yes this happened because now the subqueries used by withCount, whereHas and has methods also invokes this method. You need to wrap your code inside a conditional and it should work fine.

@manyToMany(() => User, {
  pivotTable: "forum_user",
  onQuery: (query) => {
    if (!query.isRelatedQuery) { return }
    query.apply((scopes) => scopes.byRoles(["moderator"]))
  },
})
public moderators: ManyToMany<typeof User>;

I have plans to provide separate hooks for sub-queries and related queries

Replies: 2 comments 2 replies

Comment options

You must be logged in to vote
1 reply
@Obapelumi
Comment options

Answer selected by Obapelumi
Comment options

You must be logged in to vote
1 reply
@Obapelumi
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Help
Labels
None yet
2 participants