Adonis Query Without Global Scopes

#1

Hi

I have global scope running on my model like this:

class Product extends Model {

static boot() {
	super.boot();

	this.addGlobalScope(builder => {
		builder.setVisible(["product_id","product_name"]);
		builder.with("product_details");

This still returns all product details even though I query with the specific product_details_id in my Controller.

Is there anyway to overwrite the global scope and prioritize the query I write in Controller?

Thanks in advance!

0 Likes

#2

Can you share the code you write inside your controller?

0 Likes

#3
const list = await Product.query()
		.select(["product_id", "product_name"])
		.with("product_detail", builder => {
		    builder.where("product_detail_id", 2);
	 	})
        .fetch()

It should returned child with product_detail_id of “2” only, but still returned all product details instead because got override from the model’s GlobalScope

0 Likes

#4

Instead of using Product.query(), use Product.queryWithoutScopes()

1 Like

#5

I got error telling me its not a function

"error": "Product.queryWithoutScopes is not a function"
0 Likes

#6

You got typo, it should be : queryWithOutScopes

Now it’s working flawlessly, thanks @virk :wink:
Hope this get documented also :slight_smile:

2 Likes