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

How to use query scope with hasManyDeepFromRelations? #36

Closed
Siddharaj6010 opened this issue Aug 9, 2019 · 5 comments
Closed

How to use query scope with hasManyDeepFromRelations? #36

Siddharaj6010 opened this issue Aug 9, 2019 · 5 comments

Comments

@Siddharaj6010
Copy link

Thank you for this wonderful package. I just came to know about it recently and it has already made many parts of my code simpler.

I went through the documentation but there's one problem where I would appreciate some help.

I have the following relationship:

  • Lawyer office hasMany Lawyers
  • Lawyer belongsToMany Companies
  • Company belongsToMany Lawyers
  • Company hasMany Claims

Now I was able to get the companies of the lawyer office by using hasManyDeepFromRelations as

// Inside LawyerOffice Model

public function companies()
{
    return $this->hasManyDeepFromRelations($this->lawyers(), (new Lawyer)->companies())->active();
}

But when I do the same for claims as

public function claims()
{
    return $this->hasManyDeepFromRelations($this->companies(), (new Company)->claims());
}

It ignores the active() query scope of the companies and returns claims of all the companies.

I assume this is something related to #35 but I am not sure where should I add the query scope manually.

Can you please guide me on how can I add query scope or where() condition in order to get the claims of the active companies of a lawyer office.

Thank you.

@staudenmeir
Copy link
Owner

It's not explicitly stated in the documentation, but hasManyDeepFromRelations() doesn't apply additional constraints from the original relationships.

You'll need to add the scope's constraint(s) manually:

public function claims()
{
    return $this->hasManyDeepFromRelations($this->companies(), (new Company)->claims())
        ->where('companies.active', true);
}

@Siddharaj6010
Copy link
Author

This is working exceptionally well.

Thank you for the quick response :)

@hasnatbabur
Copy link

hasnatbabur commented May 6, 2020

@staudenmeir Thanks for this awesome package.

For this situation:
"It's not explicitly stated in the documentation, but hasManyDeepFromRelations() doesn't apply additional constraints from the original relationships."

Don't you think this is violating the DRY rule? Let's say I want to get all posts which is only published most cases. So if I forgot to add the publish constraint in hasManyDeepFromRelations then different query will behave different and there is no single source of control on a relationship. A lots of bug will be left under the hood.

Do you think we can improve the issue? Thanks

@staudenmeir
Copy link
Owner

@hasnatbabur I agree, it wasn't a deliberate decision to not apply the constraints. The issue is quite complex and I'm still working on it.

I see the automatic concatenation of foreign and local keys as the main benefit of hasManyDeepFromRelation() and decided to release it without the constraint "feature".

@staudenmeir
Copy link
Owner

I released v1.15.3 with hasManyDeepFromRelationsWithConstraints():

public function claims()
{
    return $this->hasManyDeepFromRelationsWithConstraints([$this, 'companies'], [new Company(), 'claims']);
}

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

3 participants