question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Global scope of pivotModel not included in whereHas()

See original GitHub issue

This scenario requires following things:

  • model with belongsToMany relationship using pivotModel
  • pivot model which has a global query scope

The problem: queries using whereHas are not applying the global scope of a pivot model. Query generated by the builder:

select * from `users` where exists (select * from `tags` inner join `user_tags` on `tags`.`id` = `user_tags`.`tag_id` where `users`.`id` = `user_tags`.`user_id`)

should be:

select * from `users` where exists (select * from `tags` inner join `user_tags` on `tags`.`id` = `user_tags`.`tag_id` where `users`.`id` = `user_tags`.`user_id` and `user_tags`.`active` = 1)

Package version

6.1.3

Node.js and npm version

$ node --version
v10.15.2
$ npm --version
6.4.1

Sample Code (to reproduce the issue)

/** @type {typeof import('@adonisjs/lucid/src/Lucid/Model')} */
const Model = use('Model')

class Tag extends Model {
}

class User extends Model {
  tags () {
    return this.belongsToMany('App/Models/Tag')
      .pivotModel('App/Models/UserTag')
  }
}

class UserTag extends Model {
  static boot () {
    super.boot()

    this.addGlobalScope(
      query => {
        query.where('active', true)
      },
      'foo'
    )
  }
}

// the query:
User.query().whereHas('tags').first()

BONUS (a sample repo to reproduce the issue)

https://github.com/radmen/bug-adonis-where-has-pivot-scope

Setup:

npm i
adonis migration:run
adonis seed
DEBUG=knex:query adonis serve

Open: http://localhost:3333/

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
radmencommented, Mar 12, 2019

@thetutlage thanks! It solves this issue.

Btw, I have a question - is it possible to use scopes of pivotModel in whereHas query? It could be quite helpful.

A quick example (using soft-deletes package):

User.query().whereHas('posts', qb => {
  qp.withTrashed();
});
1reaction
thetutlagecommented, Mar 11, 2019

@radmen Thanks for sharing a sample repo, it helps. Want to try it from Github as see if latest commit fixes the issue for you or not?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Remove a specific global scope in a whereHas model call in ...
I was able to solve this by overriding the relating methods. I've created a trait for ease of use.
Read more >
Eloquent ORM - Laravel - The PHP Framework For Web Artisans
When querying a model that uses soft deletes, the "deleted" models will not be included in query results. Forcing Soft Deleted Models Into...
Read more >
Calling scope on pivot model in many to many relationship
I have a model for the pivot ThingUser, and a scope scopeScope on ThingUser. The scope is complicated and i do not want...
Read more >
Ordering database queries by relationship columns in Laravel
We're not trying to simply order the results of the relationship itself. ... is by using a global scope on the relationship model...
Read more >
Relationships - October CMS - 2.x
The scope parameter can also refer to a static method. ... However, if the foreign key on the Phone model is not user_id...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found