Closed Ruchira0893 closed 3 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']
);
}
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.
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.
I'm unable to get the desired result.
Are you getting an error?
No, but I'm getting projects count = 0 for each brand.
How are you using the projects
relationship in your query?
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.
What do you get when replacing ->get()
with ->toSql()
?
"select
brands
., (select count() fromprojects
inner joinconstruction_product_projects
onconstruction_product_projects
.project_id
=projects
.id
inner joinconstruction_product
onconstruction_product
.id
=construction_product_projects
.construction_product_id
inner joinconstruction_product_details
onconstruction_product_details
.id
=construction_product
.id
inner joinconstruction_product_brands
onconstruction_product_brands
.const_product_details_id
=construction_product_details
.id
whereconstruction_product
.deleted_at
is null andconstruction_product_details
.deleted_at
is null andbrands
.id
=construction_product_brands
.brand_id
andprojects
.status
= ? and exists (select * fromconstruction_product
inner joinconstruction_product_projects
onconstruction_product
.id
=construction_product_projects
.construction_product_id
whereprojects
.id
=construction_product_projects
.project_id
andstatus
= ? andconstruction_product
.deleted_at
is null) and (tracked
= ?) andconstruction_product
.deleted_at
is null andconstruction_product_details
.deleted_at
is null andprojects
.deleted_at
is null) asprojects_count
frombrands
wherebrands
.deleted_at
is null"
Sorry, I somehow didn't get a notification for your comment. Are you still having this issue?
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 isbelongsToMany
with ProjectMy model looks something like this:
My relationship looks like:
Help me out with this. Thanks.