[WIP] Refactor models to better lisibility and speed

This commit is contained in:
Ludovic CANDELLIER
2020-08-24 01:14:54 +02:00
parent 77a239fce9
commit 3b6108b449
22 changed files with 351 additions and 400 deletions

View File

@@ -7,46 +7,28 @@ use Znck\Eloquent\Traits\BelongsToThrough;
class ArticlePrice extends Model
{
use BelongsToThrough;
use BelongsToThrough;
protected $guarded = ['id'];
protected $table = 'shop_article_prices';
protected $guarded = ['id'];
protected $table = 'shop_article_prices';
public function article_attribute()
public function price()
{
return $this->hasOne('App\Models\Price');
}
public function priceFamilyValue()
{
return $this->belongsTo('App\Models\PriceFamilyValue');
}
public function scopeByQuantity($query, $quantity)
{
return $query->orderBy('quantity', 'desc')->where('quantity', '<', $quantity)->first();
}
public function scopeByPriceFamilyValue($query, $id)
{
return $this->belongsTo('App\Models\Shop\ArticleAttribute');
}
public function article()
{
return $this->belongsToThrough(
'App\Models\Shop\Article',
'App\Models\Shop\ArticleAttribute',
null,
'',
['App\Models\Shop\Article' => 'article_id', 'App\Models\Shop\ArticleAttribute' => 'article_attribute_id']
);
}
public function scopeByArticle($query, $id)
{
return $query->whereHas('article', function ($query) use ($id) {
$query->byArticle($id);
});
}
public function scopeByAttributeValue($query, $id)
{
return $query->whereHas('article_attribute', function ($query) use ($id) {
$query->byAttributeValue($id);
});
}
public function scopeByFamily($query, $id)
{
return $query->whereHas('article_attribute', function ($query) use ($id) {
$query->byFamily($id);
});
}
return $query->where('price_family_value_id', $id);
}
}