staudenmeir / eloquent-has-many-deep

Laravel Eloquent HasManyThrough relationships with unlimited levels
MIT License
2.67k stars 157 forks source link

Help with belongsToMany and belongsToMany #101

Closed Ruchira0893 closed 3 years ago

Ruchira0893 commented 4 years ago

Hello,

Thank you for such an amazing library, I have been using it for a while and got stuck with this relational structure:

I have Group which is in belongsToMany with ConstructionProduct which is belongsToMany with Project

My model looks something like this:

class Group extends Model
{

    public function constructionProduct()
    {
        return  $this->belongsToMany(ConstructionProduct::class,'construction_product_details',
            'product_group_id', 'construction_product_id');
    }

}

class ConstructionProduct extends Model {

    public function projects()
    {
        return $this->belongsToMany(Project::class,'construction_product_projects','construction_product_id','project_id');
    }

}

My relationship looks like:

public function projects()
{
    return $this->hasManyDeep(
        Project::class,
        ['construction_product_details', ConstructionProduct::class, 'construction_product_projects'],
        ['product_group_id', 'construction_product_id', 'id'],
        ['id', 'construction_product_id', 'project_id']
    );
}

Error: Unknown column 'construction_product.project_id

Help me out with this. Thanks.

staudenmeir commented 4 years ago

Please try this relationship:

public function projects()
{
    return $this->hasManyDeep(
        Project::class,
        ['construction_product_details', ConstructionProduct::class, 'construction_product_projects'],
        ['product_group_id']
    );
}
Ruchira0893 commented 4 years ago

Thanks, it worked. One more doubt how to execute belongsToMany to belongsTo to belongsToMany relationships:

I have Brand which is in belongsToMany with ConstructionProductDetails which is belongsTo with ConstructionProduct which is in belongsToMany with Project

My model looks something like this:

class Brand extends Model {

    public function constructionDetails()
    {
        return $this->belongsToMany(ConstructionProductDetails::class, 'construction_product_brands', 'brand_id', 'const_product_details_id');
    }

}

class ConstructionProductDetails extends Model {

    public function constructionProduct()
    {
        return $this->belongsTo(ConstructionProduct::class, 'construction_product_id', 'id');
    }

}

class ConstructionProduct extends Model {

    public function projects()
    {
        return $this->belongsToMany(Project::class,'construction_product_projects','construction_product_id','project_id');
    }

}

Help me out with this. Thanks.

staudenmeir commented 4 years ago

Have you tried https://github.com/staudenmeir/eloquent-has-many-deep#existing-relationships?

Ruchira0893 commented 4 years ago

I have tried this out but didn't get the expected result.

I tried like this:

class ConstructionProductDetails extends Model {

    public function projects()
    {
        return $this->hasManyDeep(
            Project::class,
            [ConstructionProduct::class, 'construction_product_projects'],
            ['id']
        );
    }
}

And my Brand model:

class Brand extends Model {

    public function constructionDetails()
    {
        return $this->belongsToMany(ConstructionProductDetails::class, 'construction_product_brands', 'brand_id', 'const_product_details_id');
    }

    //Relation I tried.
    public function projects()
    {
        return $this->hasManyDeepFromRelations(
            $this->constructionDetails(), (new ConstructionProductDetails())->projects()
        );
    }

}

I'm unable to get the desired result.

staudenmeir commented 4 years ago

I'm unable to get the desired result.

Are you getting an error?

Ruchira0893 commented 4 years ago

No, but I'm getting projects count = 0 for each brand.

staudenmeir commented 4 years ago

How are you using the projects relationship in your query?

Ruchira0893 commented 4 years ago

Hi, sorry for late response, I'm using projects_count with Brand Model like this:

$brands = Brands::withCount(['projects' => funtion ($q) {
        //some logic over here....
    }])->get();

Whenever I'm trying to fetch projects_count I find it to be null/0.

staudenmeir commented 4 years ago

What do you get when replacing ->get() with ->toSql()?

Ruchira0893 commented 4 years ago

"select brands., (select count() from projects inner join construction_product_projects on construction_product_projects.project_id = projects.id inner join construction_product on construction_product.id = construction_product_projects.construction_product_id inner join construction_product_details on construction_product_details.id = construction_product.id inner join construction_product_brands on construction_product_brands.const_product_details_id = construction_product_details.id where construction_product.deleted_at is null and construction_product_details.deleted_at is null and brands.id = construction_product_brands.brand_id and projects.status = ? and exists (select * from construction_product inner join construction_product_projects on construction_product.id = construction_product_projects.construction_product_id where projects.id = construction_product_projects.project_id and status = ? and construction_product.deleted_at is null) and (tracked = ?) and construction_product.deleted_at is null and construction_product_details.deleted_at is null and projects.deleted_at is null) as projects_count from brands where brands.deleted_at is null"

staudenmeir commented 3 years ago

Sorry, I somehow didn't get a notification for your comment. Are you still having this issue?