[WIP] Add thumb on offers, refactor categories, try to fix counter on relations polymorphic with eage loader, bad pattern !

This commit is contained in:
Ludovic CANDELLIER
2021-12-17 00:30:07 +01:00
parent cb0b2e4aa0
commit 8a1573d425
26 changed files with 171 additions and 107 deletions

View File

@@ -0,0 +1,16 @@
<?php
namespace App\Models\Core;
use Rinvex\Categories\Models\Category as parentCategory;
use App\Models\Shop\Article;
class Category extends parentCategory
{
public function Articles()
{
// return $this->entries(Article::class);
return $this->morphedByMany(Article::class, 'categorizable');
}
}

View File

@@ -8,18 +8,19 @@ use Illuminate\Database\Eloquent\SoftDeletes;
use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;
use Spatie\MediaLibrary\MediaCollections\Models\Media;
use Spatie\Translatable\HasTranslations;
// use Rinvex\Categories\Traits\Categorizable;
use Rinvex\Tags\Traits\Taggable;
use Fico7489\Laravel\EloquentJoin\Traits\EloquentJoin;
use Wildside\Userstamps\Userstamps;
class Category extends Model
{
use InteractsWithMedia, SoftDeletes, Taggable, Userstamps;
use HasTranslations, InteractsWithMedia, SoftDeletes, Taggable, Userstamps;
protected $guarded = ['id'];
protected $table = 'shop_categories';
protected $table = 'categories';
public $translatable = ['name','description'];
public function Articles()
{
@@ -31,14 +32,9 @@ class Category extends Model
return $this->tags->articles;
}
public function Shelves()
public function countArticlesTagged()
{
return $this->morphedByMany(Category::class, 'categorizable');
}
public function CategoryTree()
{
return $this->belongsTo(app('rinvex.categories.category'), 'category_id');
return $this->tags()->withCount('Articles');
}
public function scopeByCategory($query, $category_id)

View File

@@ -23,9 +23,9 @@ class Offer extends Model
return $this->article->categories();
}
public function variation()
public function tags()
{
return $this->belongsTo(Variation::class);
return $this->article->tags();
}
public function tariff()
@@ -33,6 +33,11 @@ class Offer extends Model
return $this->belongsTo(Tariff::class);
}
public function variation()
{
return $this->belongsTo(Variation::class);
}
public function scopeByArticle($query, $id)
{
return $query->where('article_id', $id);
@@ -64,6 +69,20 @@ class Offer extends Model
return $query->where('status_id', $id);
}
public function scopeByTag($query, $tag_id)
{
return $query->whereHas('article.tags', function ($query) use ($tag_id) {
$query->where('tag_id', $tag_id);
});
}
public function scopeByTags($query, $tags)
{
return $query->whereHas('article.tags', function ($query) use ($tags) {
$query->whereIn('tag_id', $tags);
});
}
public function scopeByVariation($query, $id)
{
return $query->where('variation_id', $id);

View File

@@ -24,6 +24,12 @@ class Tag extends parentTag
}
*/
// TODO
public function offers()
{
return $this->articles();
}
public function articles()
{
return $this->morphedByMany(Article::class, 'taggable');