[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

@@ -48,7 +48,7 @@ class Article extends Model implements HasMedia
public function prices()
{
return $this->hasManyThrough('App\Models\Shop\ArticlePrice','App\Models\Shop\ArticleAttribute');
return $this->hasMany('App\Models\Shop\Price');
}
public function product()

View File

@@ -1,43 +0,0 @@
<?php
namespace App\Models\Shop;
use Illuminate\Database\Eloquent\Model;
class ArticleAttribute extends Model
{
protected $guarded = ['id'];
protected $table = 'shop_article_attributes';
public function article()
{
return $this->belongsTo('App\Models\Shop\Article');
}
public function attribute_value()
{
return $this->belongsTo('App\Models\Shop\ArticleAttributeValue', 'article_attribute_value_id');
}
public function prices()
{
return $this->hasMany('App\Models\Shop\ArticlePrice');
}
public function scopeByArticle($query, $id)
{
return $query->where('article_id', $id);
}
public function scopeByAttributeValue($query, $id)
{
return $query->where('attribute_value_id', $id);
}
public function scopeByFamily($query, $id)
{
return $query->whereHas('attribute_value', function ($query) use ($id) {
$query->byFamily($id);
});
}
}

View File

@@ -1,25 +0,0 @@
<?php
namespace App\Models\Shop;
use Illuminate\Database\Eloquent\Model;
use Staudenmeir\EloquentHasManyDeep\HasRelationships;
class ArticleAttributeFamily extends Model
{
use HasRelationships;
protected $guarded = ['id'];
protected $table = 'shop_article_attribute_families';
public function values()
{
return $this->hasMany('App\Models\Shop\ArticleAttributeValue');
}
public function articles()
{
return $this->hasManyThrough('App\Models\Shop\ArticleAttribute','App\Models\Shop\ArticleAttributeValue');
}
}

View File

@@ -1,32 +0,0 @@
<?php
namespace App\Models\Shop;
use Illuminate\Database\Eloquent\Model;
class ArticleAttributeValue extends Model
{
protected $guarded = ['id'];
protected $table = 'shop_article_attribute_values';
public function article_attribute_family()
{
return $this->belongsTo('App\Models\Shop\ArticleAttributeFamily');
}
public function attributes()
{
return $this->hasMany('App\Models\Shop\ArticleAttribute');
}
public function articles()
{
return $this->hasMany('App\Models\Shop\ArticleAttribute')->groupBy('article_id');
}
public function scopeByFamily($query, $id)
{
return $query->where('article_attribute_family_id', $id);
}
}

View File

@@ -1,31 +0,0 @@
<?php
namespace App\Models\Shop;
use Illuminate\Database\Eloquent\Model;
class ArticleCategory extends Model
{
protected $guarded = ['id'];
protected $table = 'shop_article_categories';
public function article()
{
return $this->belongsTo('App\Models\Shop\Article');
}
public function category()
{
return $this->belongsTo('App\Models\Shop\Category');
}
public function scopeByArticle($query, $id)
{
return $query->where('article_id', $id);
}
public function scopeByCategory($query, $id)
{
return $query->where('category_id', $id);
}
}

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);
}
}

View File

@@ -1,20 +0,0 @@
<?php
namespace App\Models\Shop;
use Illuminate\Database\Eloquent\Model;
use Znck\Eloquent\Traits\BelongsToThrough;
class ArticlePriceGeneric extends Model
{
use BelongsToThrough;
protected $guarded = ['id'];
protected $table = 'shop_article_price_generics';
public function article_prices()
{
return $this->hasMany('App\Models\Shop\ArticlePrice');
}
}

View File

@@ -1,18 +0,0 @@
<?php
namespace App\Models\Shop;
use Illuminate\Database\Eloquent\Model;
class Inventory extends Model
{
protected $guarded = ['id'];
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function InventoryPlace()
{
return $this->belongsTo('App\Models\Shop\InventoryPlace');
}
}

View File

@@ -1,18 +0,0 @@
<?php
namespace App\Models\Shop;
use Illuminate\Database\Eloquent\Model;
class InventoryPlace extends Model
{
protected $guarded = ['id'];
/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function Inventories()
{
return $this->hasMany('App\Models\Inventory');
}
}

40
app/Models/Shop/Price.php Normal file
View File

@@ -0,0 +1,40 @@
<?php
namespace App\Models\Shop;
use Illuminate\Database\Eloquent\Model;
use Staudenmeir\EloquentHasManyDeep\HasRelationships;
class Price extends Model
{
use HasRelationships;
protected $guarded = ['id'];
protected $table = 'shop_prices';
public function article()
{
return $this->belongsTo('App\Models\Shop\Article');
}
public function price_family()
{
return $this->belongsTo('App\Models\Shop\PriceFamily');
}
public function price()
{
return $this->morphTo();
}
public function scopeByArticle($query, $id)
{
return $query->where('article_id', $id);
}
public function scopeByPriceFamily($query, $id)
{
return $query->where('price_family_id', $id);
}
}

View File

@@ -0,0 +1,30 @@
<?php
namespace App\Models\Shop;
use Illuminate\Database\Eloquent\Model;
use Staudenmeir\EloquentHasManyDeep\HasRelationships;
class PriceFamily extends Model
{
use HasRelationships;
protected $guarded = ['id'];
protected $table = 'shop_price_families';
public function prices()
{
return $this->hasMany('App\Models\Shop\Price');
}
public function values()
{
return $this->hasMany('App\Models\Shop\PriceFamilyValue');
}
public function articles()
{
return $this->hasManyThrough('App\Models\Shop\Article','App\Models\Shop\Price');
}
}

View File

@@ -0,0 +1,22 @@
<?php
namespace App\Models\Shop;
use Illuminate\Database\Eloquent\Model;
class PriceFamilyValue extends Model
{
protected $guarded = ['id'];
protected $table = 'shop_price_familiy_values';
public function price_family()
{
return $this->belongsTo('App\Models\Shop\PriceFamily');
}
public function scopeByFamily($query, $id)
{
return $query->where('price_family_id');
}
}

View File

@@ -0,0 +1,30 @@
<?php
namespace App\Models\Shop;
use Illuminate\Database\Eloquent\Model;
use Znck\Eloquent\Traits\BelongsToThrough;
class PriceGeneric extends Model
{
use BelongsToThrough;
protected $guarded = ['id'];
protected $table = 'shop_price_generics';
public function prices()
{
return $this->hasMany('App\Models\Shop\Price');
}
public function values()
{
return $this->hasMany('App\Models\Shop\PriceGenericValue');
}
public function articles()
{
return $this->hasManyThrough('App\Models\Shop\Article','App\Models\Shop\Price');
}
}

View File

@@ -0,0 +1,30 @@
<?php
namespace App\Models\Shop;
use Illuminate\Database\Eloquent\Model;
use Znck\Eloquent\Traits\BelongsToThrough;
class PriceGenericValue extends Model
{
use BelongsToThrough;
protected $guarded = ['id'];
protected $table = 'shop_price_generic_values';
public function price_generic()
{
return $this->belongsTo('App\Models\Shop\PriceGeneric');
}
public function scopeByPriceGeneric($query, $id)
{
return $query->where('price_generic_id', $id);
}
public function scopeByQuantity($query, $quantity)
{
return $query->orderBy('quantity', 'desc')->where('quantity', '<', $quantity)->first();
}
}