Jan 29, 2024
0
Level 2
HasMany relation on a casted property
I'm trying to create a HasMany relationship on an attribute that is casted to an array like this
public function shopify_products(): HasMany
{
return new HasMany(ShopifyProduct::whereIn('sku', $this->sku), $this, 'sku', 'sku');
}
In the 'parent' model the sku attribute is casted to an array, while in the ShopifyProcut model it is a normal string.
It seems to work when the array only contains one element, but fails to query all skus when it contains multiple element
I can do
public function shopify_products()
{
return ShopifyProduct::whereIn('sku', $this->sku)->get();
}
but it feels more natural to handle it through a relationship ....
Please or to participate in this conversation.