Add deep relations

This commit is contained in:
Ludovic CANDELLIER
2022-01-14 00:03:21 +01:00
parent b2f5cc4a45
commit b1a2e70d12
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);
}
}