Код: Выделить всё
public function getAllVariants(): HasMany
{
return $this->hasMany(ProductVariant::class, 'product_id', 'id')->with(['product', 'attribute_values', 'image', 'description']);
}
Код: Выделить всё
public function getAllVariants(): HasMany
{
$allVariants = $this->hasMany(ProductVariant::class, 'product_id', 'id')->with(['product', 'attribute_values', 'image', 'description']);
if ($allVariants->count() === 0) {
$variantByCode = ProductVariant::where('code', $this->code)->first();
if (!$variantByCode) {
$variants = $this->hasMany(ProductVariant::class, 'product_id', 'id')->where('id', '=', 0);
}
// Retrieve variants related to the product
$allVariants = ProductVariant::where('product_id', $variantByCode->product_id)
->with(['product', 'attribute_values', 'image', 'description']);
}
return $allVariants;
}
Возвращаемое значение Blockquote должно иметь тип Illuminate\Database\Eloquent\Relations\HasMany, возвращается Illuminate\Database\Eloquent\Builder
Также хочу отметить, что в дальнейшем этот код будет использоваться как:
Код: Выделить всё
$variants = $this->getAllVariants
->each(fn(ProductVariant $variant) => $variant->setRelation('product', $this))
->sortBy('position');
Подробнее здесь: https://stackoverflow.com/questions/787 ... asmany-ill