Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

peterhrobar's avatar

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 ....

0 likes
0 replies

Please or to participate in this conversation.