I have ProductCollection
Model and AttributeValue
Model which have a common table product_collection_attributes
that has 2 columns: product_collection_id
and attribute_value_id
that are a foreign key to their respective tables and are both a primary key.
In ProductCollection Model relation is:
public function attributes()
{
return $this->belongsToMany(AttributeValue::class, 'product_collection_attributes', 'product_collection_id');
}
And in AttributeValue Model relation is:
public function product_collections()
{
return $this->belongsToMany(ProductCollection::class, 'product_collection_attributes', 'attribute_value_id');
}
My query is: $productCollection->attributes()->get()
; which is returning an empty array,
However if query was DB::table('product_collection_attributes')->where('product_collection_id', $productCollection->id)->get();
its returning a full array!!
I don’t know why and it is important to use ProductCollection::with(['attributes'])
in almost all of my cases specially with joins, and it is forcing me to use DB instead!
How can we fix that?