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

Add support for filtering data based upon relations #92

Closed
thetutlage opened this issue Jan 25, 2017 · 1 comment
Closed

Add support for filtering data based upon relations #92

thetutlage opened this issue Jan 25, 2017 · 1 comment
Assignees

Comments

@thetutlage
Copy link
Member

One should be able to filter top level rows by adding clauses on relationships. For example:

Fetch all posts where comments are greater than 3.

yield Post.query().has('comments', '>', 3).fetch()

Required Method

  1. has
  2. whereHas
  3. doesntHave
  4. whereDoesntHave
  5. withCount
@thetutlage
Copy link
Member Author

thetutlage commented Jan 25, 2017

Original Thread

by @KungWaz

It would be nice to be able to filter the top level rows when querying models with relations.

If I have this structure:

office: {
  orders: [{
    items: [{
      status: 'p'
    }, {
      status: 'c'
    }]
  },{
    items: [{
      status: 'd'
    }]
  }]
  },{
    items: [{
      status: 'd'
    }]
  }]
  }]]
}

I want to get this result:

office: {
  orders: [{
    items: [{
      status: 'p'
    }, {
      status: 'c'
    }]
  }]]
}

But at the moment I can only accomplish this:

office: {
  orders: [{
    items: [{
      status: 'p'
    }, {
      status: 'c'
    }]
  },{
    items: []
  }]
  },{
    items: []
  }]
  }]]
}

I use the query builder with scoping:

const office = yield Office
  .query()
  .with('orders.items')
  .where('id', officeId)
  .scope('orders.items', (builder) => {
    builder.whereNot('status','d')
  })
  .first()

It would be nice to use a whereHas function here to be able to only get Orders that have OrderItems.

This request is related to this question on SO:
http://stackoverflow.com/questions/41679272/is-it-possible-to-do-a-nested-query-builder-in-adonisjs-with-lucid/41715041#41715041

thetutlage added a commit that referenced this issue Jul 16, 2017
add support for filtering top level results when something is truthy with their related models. Add

methods {has, whereHas, doesntHave, whereDoesHave, withCount}

Closes #92
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