fixes
This commit is contained in:
@@ -191,6 +191,10 @@ class Article extends Model implements HasMedia
|
||||
{
|
||||
return $tagId ? $query->whereHas('tags', function ($query) use ($tagId) {
|
||||
$query->byId($tagId);
|
||||
})->orWhereHasMorph('product', [Variety::class], function($query) use ($tagId) {
|
||||
$query->whereHas('tags', function ($query) use ($tagId) {
|
||||
$query->where('id', $tagId);
|
||||
});
|
||||
}) : $query;
|
||||
}
|
||||
|
||||
@@ -198,6 +202,10 @@ class Article extends Model implements HasMedia
|
||||
{
|
||||
return $tags ? $query->whereHas('tags', function ($query) use ($tags) {
|
||||
$query->byIds($tags);
|
||||
})->orWhereHasMorph('product', [Variety::class], function($query) use ($tags) {
|
||||
$query->whereHas('tags', function ($query) use ($tags) {
|
||||
$query->whereIntegerInRaw('id', $tags);
|
||||
});
|
||||
}) : $query;
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Models\Shop;
|
||||
|
||||
use App\Repositories\Core\DateTime;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Venturecraft\Revisionable\RevisionableTrait;
|
||||
@@ -47,4 +48,14 @@ class InvoicePayment extends Model
|
||||
{
|
||||
return $query->where('payment_type', $paymentType);
|
||||
}
|
||||
|
||||
public function getDateAttribute($value)
|
||||
{
|
||||
return DateTime::dateToLocale($value);
|
||||
}
|
||||
|
||||
public function setDateAttribute($value)
|
||||
{
|
||||
$this->attributes['date'] = DateTime::convert($value);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ use App\Models\Botanic\Specie;
|
||||
use App\Models\Botanic\Variety;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\MorphToMany;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Rinvex\Tags\Models\Tag as parentTag;
|
||||
|
||||
class Tag extends parentTag
|
||||
@@ -78,6 +79,65 @@ class Tag extends parentTag
|
||||
]);
|
||||
}
|
||||
|
||||
public static function countArticles($categoryId)
|
||||
{
|
||||
DB::table('tags')
|
||||
// Articles directement liés au tag, filtrés par catégorie
|
||||
->leftJoin('taggables as direct', function ($join) {
|
||||
$join->on('tags.id', '=', 'direct.tag_id')
|
||||
->where('direct.taggable_type', '=', Article::class);
|
||||
})
|
||||
->leftJoin('shop_articles', 'direct.taggable_id', '=', 'shop_articles.id')
|
||||
->whereExists(function ($query) use ($categoryId) {
|
||||
$query->select(DB::raw(1))
|
||||
->from('categories')
|
||||
->whereColumn('categories.id', 'shop_articles.category_id')
|
||||
->where('_lft', '>=', DB::raw("(SELECT _lft FROM categories WHERE id = {$categoryId})"))
|
||||
->where('_rgt', '<=', DB::raw("(SELECT _rgt FROM categories WHERE id = {$categoryId})"));
|
||||
})
|
||||
|
||||
// Articles liés via une variété ayant le tag, filtrés par catégorie
|
||||
->leftJoin('taggables as via_variety', function ($join) {
|
||||
$join->on('tags.id', '=', 'via_variety.tag_id')
|
||||
->where('via_variety.taggable_type', '=', Variety::class);
|
||||
})
|
||||
->leftJoin('botanic_varieties', 'via_variety.taggable_id', '=', 'botanic_varieties.id')
|
||||
->leftJoin('shop_articles as indirect_articles', function ($join) {
|
||||
$join->on('varieties.id', '=', 'indirect_articles.product_id')
|
||||
->where('indirect_articles.product_type', '=', Variety::class);
|
||||
})
|
||||
->whereExists(function ($query) use ($categoryId) {
|
||||
$query->select(DB::raw(1))
|
||||
->from('categories')
|
||||
->whereColumn('categories.id', 'indirect_articles.category_id')
|
||||
->where('_lft', '>=', DB::raw("(SELECT _lft FROM categories WHERE id = {$categoryId})"))
|
||||
->where('_rgt', '<=', DB::raw("(SELECT _rgt FROM categories WHERE id = {$categoryId})"));
|
||||
})
|
||||
|
||||
// Combinaison des deux types de liens et comptage
|
||||
->select('tags.id', 'tags.name', DB::raw('COUNT(DISTINCT shop_articles.id) + COUNT(DISTINCT indirect_articles.id) as article_count'))
|
||||
->groupBy('tags.id', 'tags.name')
|
||||
->get();
|
||||
}
|
||||
|
||||
public function scopeWithFilteredArticleCounts($query, $categoryId)
|
||||
{
|
||||
return $query->withCount([
|
||||
// Articles directement liés au tag et filtrés par catégorie
|
||||
'articles as direct_article_count' => function ($query) use ($categoryId) {
|
||||
$query->byCategoryParent($categoryId);
|
||||
},
|
||||
|
||||
// Articles liés via Variety et filtrés par catégorie sur les articles eux-mêmes
|
||||
'articles as indirect_article_count' => function ($query) use ($categoryId) {
|
||||
$query->whereHasMorph('product', [Variety::class], function ($subQuery) {
|
||||
// Pas de catégorie sur Variety, pas de filtre ici
|
||||
})->byCategoryParent($categoryId);
|
||||
},
|
||||
])
|
||||
->havingRaw('(COALESCE(direct_article_count, 0) + COALESCE(indirect_article_count, 0)) > 0'); // Filtre les tags avec au moins un article
|
||||
}
|
||||
|
||||
public function scopeById($query, $id)
|
||||
{
|
||||
return $query->where($this->table.'.id', $id);
|
||||
@@ -85,6 +145,6 @@ class Tag extends parentTag
|
||||
|
||||
public function scopeByIds($query, $ids)
|
||||
{
|
||||
return $query->whereIn($this->table.'.id', $ids);
|
||||
return $query->whereIntegerInRaw($this->table.'.id', $ids);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user