Add deep relations

This commit is contained in:
Ludovic CANDELLIER
2022-01-14 00:03:21 +01:00
parent 95ca3c6404
commit 050fd76122
13 changed files with 159 additions and 30 deletions

View File

@@ -11,13 +11,14 @@ use Rinvex\Tags\Traits\Taggable;
use Kirschbaum\PowerJoins\PowerJoins;
use Fico7489\Laravel\EloquentJoin\Traits\EloquentJoin;
use Wildside\Userstamps\Userstamps;
use Staudenmeir\EloquentHasManyDeep\HasRelationships;
use App\Traits\Model\HasComments;
use App\Traits\Model\Imageable;
class Article extends Model implements HasMedia
{
use Categorizable, EloquentJoin, HasComments, Imageable, Powerjoins, Taggable, SoftDeletes, UserStamps;
use Categorizable, EloquentJoin, HasComments, HasRelationships, Imageable, Powerjoins, Taggable, SoftDeletes, UserStamps;
protected $guarded = ['id'];
protected $table = 'shop_articles';
@@ -37,10 +38,12 @@ class Article extends Model implements HasMedia
return $this->hasMany(Offer::class);
}
/*
public function prices()
{
return $this->hasMany(Price::class);
return $this->hasManyDeep(PriceListValue::class, [Offer::class, Tariff::class, PriceList::class]);
}
*/
public function product()
{
@@ -52,6 +55,12 @@ class Article extends Model implements HasMedia
return $this->morphToMany(Tag::class, 'taggable');
}
public function tariffs()
{
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);
@@ -89,10 +98,15 @@ class Article extends Model implements HasMedia
return $query->has('offers');
}
public function scopeWithCurrentOffers($query)
public function scopeWithAvailableOffers($query)
{
return $query->whereHas('offers', function ($query) {
$query->where('status_id', 1);
});
}
public function scopeVisible($query)
{
return $query->where($this->table . '.visible', 1);
}
}

View File

@@ -18,16 +18,6 @@ class Offer extends Model
return $this->belongsTo(Article::class);
}
public function categories()
{
return $this->article->categories();
}
public function tags()
{
return $this->article->tags();
}
public function tariff()
{
return $this->belongsTo(Tariff::class);
@@ -38,14 +28,54 @@ class Offer extends Model
return $this->belongsTo(Variation::class);
}
public function price_lists()
{
return $this->hasManyThrough(PriceList::class, Tariff::class, 'id', 'tariff_id', 'id', 'id');
}
public function categories()
{
return $this->article->categories();
}
public function tags()
{
return $this->article->tags();
}
public function get_price_lists()
{
return $this->tariff->price_lists();
}
public function get_price_list_values()
{
return $this->tariff->price_lists->price_list_values();
}
public function scopeActive($query)
{
return $query->where('status_id', 1);
return $query->where($this->table . '.status_id', 1);
}
public function scopeByArticle($query, $id)
{
return $query->where('article_id', $id);
return $query->where($this->table . '.article_id', $id);
}
public function scopeByOffer($query, $id)
{
return $query->where($this->table . '.id', $id);
}
public function scopeByStatus($query, $id)
{
return $query->where($this->table . '.status_id', $id);
}
public function scopeByVariation($query, $id)
{
return $query->where($this->table . '.variation_id', $id);
}
public function scopeByArticleNature($query, $article_nature_id)
@@ -69,11 +99,6 @@ class Offer extends Model
});
}
public function scopeByStatus($query, $id)
{
return $query->where('status_id', $id);
}
public function scopeByTag($query, $tag_id)
{
return $query->whereHas('article.tags', function ($query) use ($tag_id) {
@@ -88,8 +113,4 @@ class Offer extends Model
});
}
public function scopeByVariation($query, $id)
{
return $query->where('variation_id', $id);
}
}

View File

@@ -20,6 +20,11 @@ class PriceList extends Model
return $this->belongsTo(Tariff::class);
}
public function offers()
{
return $this->hasManyThrough(Offer::class, Tariff::class, 'id', 'tariff_id', 'id', 'id');
}
public function sale_channel()
{
return $this->belongsTo(SaleChannel::class);
@@ -44,4 +49,11 @@ class PriceList extends Model
{
return $query->where($this->table . '.status_id', $id);
}
public function scopeByOffer($query, $id)
{
return $query->whereHas('offers', function ($query) use ($id) {
$query->byOffer($id);
});
}
}

View File

@@ -20,6 +20,20 @@ class PriceListValue extends Model
return $this->belongsTo(PriceList::class);
}
public function tariff()
{
return $this->belongsToThrough(
'App\Models\Shop\Tariff',
'App\Models\Shop\PriceList',
null,
'',
[
'App\Models\Shop\Tariff' => 'tariff_id',
'App\Models\Shop\PriceList' => 'price_list_id'
]
);
}
public function scopeByPriceList($query, $id)
{
return $query->where($this->table . '.price_list_id', $id);

View File

@@ -15,6 +15,11 @@ class Tariff extends Model
protected $guarded = ['id'];
protected $table = 'shop_tariffs';
public function offers()
{
return $this->hasMany(Offer::class);
}
public function sale_channel()
{
@@ -37,6 +42,11 @@ class Tariff extends Model
return $this->hasMany(PriceList::class);
}
public function price_list_values()
{
return $this->hasManyThrough(PriceListValue::class, PriceList::class);
}
public function scopeByAutocomplete($query, $str)
{
return $query->where($this->table . '.name', 'LIKE', "%${str}%")
@@ -53,4 +63,11 @@ class Tariff extends Model
{
return $query->where($this->table . '.status_id', $id);
}
public function scopeByOffer($query, $id)
{
return $query->whereHas('offers', function ($query) use ($id) {
$query->where('id', $id);
});
}
}