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

BelongToMany to belongsTo Relationship #37

Closed
nitish1986 opened this issue Aug 27, 2019 · 2 comments
Closed

BelongToMany to belongsTo Relationship #37

nitish1986 opened this issue Aug 27, 2019 · 2 comments

Comments

@nitish1986
Copy link

nitish1986 commented Aug 27, 2019

Hello,

I'm having following models and table name:

Tags: table name (project_tags)

id
name

which is related to TechnicalDescription model with many to many relationship:

TechnicalDescription: table name(project_technical_detail)

id
project_id
description

Many to many relation holds on table (technical_detail_tag_relation):

id
technical_detail_id
tag_id

And finally I have Project Model table name(projects)

id
name

So I have Tags -> belongsToMany -> TechnicalDescription -> belongsTo -> Project

I tried doing:

public function projects()
{
    return $this->hasManyDeep(
        Project::class,
        ['technical_detail_tag_relation', TechnicalDescription::class],
        [null, null, 'id'],
        [null, null, 'project_id']
    );
}

But it throws error:

Column not found: 1054 Unknown column 'technical_detail_tag_relation.technical_description_id' in 'on clause'

Can you help me out with it, I tried replacing technical_description_id almost every null place where I defined this relationship, but no luck.

I'm trying to learn about your package, but little confused with this kind of relationships.

Thanks.

@nitish1986
Copy link
Author

I tried fixing this:

public function projects()
{
    return $this->hasManyDeep(
        Project::class,
        ['technical_detail_tag_relation', TechnicalDescription::class],
        ['tag_id', 'id', 'id'],
        ['id', 'technical_detail_id', 'id']
    );
}

I think it is working. Just need confirmation about it. Thanks.

@staudenmeir
Copy link
Owner

You are missing the project_id column:

public function projects()
{
    return $this->hasManyDeep(
        Project::class,
        ['technical_detail_tag_relation', TechnicalDescription::class],
        ['tag_id', 'id', 'id'],
        ['id', 'technical_detail_id', 'project_id']
    );
}

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

2 participants