belongsTo(ArticleNature::class); } public function invoiceItems(): HasMany { return $this->hasMany(InvoiceItem::class); } public function offers(): HasMany { return $this->hasMany(Offer::class); } public function price_lists(): HasManyDeep { return $this->hasManyDeep( PriceList::class, [Offer::class, Tariff::class], ['article_id', 'id'], ['id', 'tariff_id'], ); } public function prices(): HasManyDeep { return $this->hasManyDeep( PriceListValue::class, [Offer::class, Tariff::class, PriceList::class], ['article_id', 'id', 'tariff_id'], ['id', 'tariff_id', 'id'], ); } public function product(): MorphTo { return $this->morphTo(); } public function siblings(): HasMany { return $this->hasMany(Article::class, 'name', 'name'); } public function tags(): MorphToMany { return $this->morphToMany(Tag::class, 'taggable'); } public function tariffs(): HasManyThrough { return $this->hasManyThrough(Tariff::class, Offer::class, 'article_id', 'id', 'id', 'tariff_id'); // return $this->belongsToMany(Tariff::class, Offer::$table, 'id', 'id', 'tariff_id', 'tariff_id'); } public function scopeByArticle($query, $id) { return $query->where($this->table.'.id', $id); } public function scopeByAutocomplete($query, $str) { return $query->where($this->table.'.name', 'LIKE', '%'.$str.'%'); } public function scopeSearch($query, $str) { return $query->where($this->table.'.name', 'LIKE', '%'.$str.'%'); } public function scopeByArticleNature($query, $id) { return $id ? $query->where($this->table.'.article_nature_id', $id) : $query; } public function scopeByArticleNatures($query, $ids) { return $ids ? $query->whereIn($this->table.'.article_nature_id', $ids) : $query; } public function scopeByCategories($query, $categoriesId) { return $categoriesId ? $query->whereHas('categories', function ($query) use ($categoriesId) { $query->whereIn('id', $categoriesId); }) : $query; } public function scopeByCategoryParent($query, $categoryId) { $category = Category::find($categoryId); return $categoryId ? $query->whereHas('categories', function ($query) use ($category) { $query->where('_lft', '>=', $category->_lft)->where('_rgt', '<=', $category->_rgt); }) : $query; } public function scopeByCategory($query, $categoryId) { return $categoryId ? $query->whereHas('categories', function ($query) use ($categoryId) { $query->where('id', $categoryId); }) : $query; } public function scopeBotanic($query) { return $query->whereIn($this->table.'.product_type', [Variety::class, Specie::class]); } public function scopeMerchandise($query) { return $query->byProduct(Merchandise::class); } public function scopeByProduct($query, $model) { return $model ? $query->where($this->table.'.product_type', $model) : $query; } public function scopeByProductId($query, $productId) { return $productId ? $query->where($this->table.'.product_id', $productId) : $query; } public function scopeByTag($query, $tagId) { return $tagId ? $query->whereHas('tags', function ($query) use ($tagId) { $query->where('id', $tagId); }) : $query; } public function scopeByTags($query, $tags) { return $tags ? $query->whereHas('tags', function ($query) use ($tags) { $query->whereIn('id', $tags); }) : $query; } public function scopeByTagsSelected($query, $tags) { return $tags ? $query->whereHas('tags', function ($query) use ($tags) { foreach ($tags as $tag) { $query->where('id', $tag); } }) : $query; } public function scopeWithOffers($query) { return $query->has('offers'); } public function scopeWithAvailableOffers($query, $saleChannelId = false) { return $query->whereHas('offers', function ($query) use ($saleChannelId) { $query->active()->byStockAvailable()->bySaleChannel($saleChannelId); }); } public function scopeVisible($query) { return $query->where($this->table.'.visible', 1); } public function scopeHomepage($query) { return $query->where($this->table.'.homepage', 1); } }