Fixes on new model

This commit is contained in:
Ludovic CANDELLIER
2020-07-26 23:17:49 +02:00
parent 4de381db06
commit 71e27ce9c3
19 changed files with 116 additions and 55 deletions

View File

@@ -13,30 +13,15 @@ class ArticlesDataTable extends DataTable
public function query(Article $model)
{
// $model = $model::with('Family')->select('shop_articles.*','family.name as family_name')->join('shop_article_families as family', 'family.id', '=', 'shop_articles.article_family_id')->groupBy('shop_articles.id');
$model = $model::with('Family')->select('shop_articles.*');
$model = $model::with('article_family')->select('shop_articles.*');
// $model = $model::joinRelations('Family')->select('shop_articles.*','shop_article_families.name as family_name');
return self::buildQuery($model);
}
protected function getColumns()
{
/*
$columns = [];
$columns[] = [
'name' => 'family',
'data' => 'family.name',
'title' => 'Famille',
'orderable' => false
];
$columns[] = [
'name' => 'name',
'title' => 'Nom',
];
$columns[] = self::makeColumnButtons();
return $columns;
*/
return [
Column::make('family.name')->title('Famille')->orderable(false),
Column::make('article_family.name')->title('Famille')->orderable(false),
Column::make('name')->title('Nom'),
self::makeColumnButtons(),
];

View File

@@ -12,7 +12,7 @@ class ArticleAttributeFamilyController extends Controller
{
public function index(ArticleAttributeFamiliesDataTable $dataTable)
{
return $dataTable->render('Shop.Admin.ArticleAttributeFamilies.index');
return $dataTable->render('Shop.Admin.ArticleAttributeFamilies.list');
}
public function getDatatable(Request $request)

View File

@@ -46,4 +46,9 @@ class Article extends Model implements HasMedia
return $this->belongsTo($this->model, 'model_id');
}
public function scopeByArticle($query, $id)
{
return $query->where('shop_articles.id',$id);
}
}

View File

@@ -3,9 +3,12 @@
namespace App\Models\Shop;
use Illuminate\Database\Eloquent\Model;
use Znck\Eloquent\Traits\BelongsToThrough;
class ArticlePrice extends Model
{
use BelongsToThrough;
protected $guarded = ['id'];
protected $table = 'shop_article_prices';
@@ -16,7 +19,21 @@ class ArticlePrice extends Model
public function article()
{
return $this->belongsTo('App\Models\Shop\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);
});
}
}

View File

@@ -12,8 +12,7 @@ use Fico7489\Laravel\EloquentJoin\Traits\EloquentJoin;
class Category extends Model
{
use Taggable;
use HasMediaTrait;
use Taggable, HasMediaTrait;
protected $guarded = ['id'];
protected $table = 'shop_categories';

View File

@@ -25,17 +25,27 @@ class ArticleAttributes
return ArticleAttribute::find($id);
}
public static function storeAttributes($article_price_id, $attributes)
public static function getByArticle($id)
{
return ArticleAttribute::byArticle($id)->get();
}
public static function getByArticleWithPrices($id)
{
return ArticleAttribute::with('prices')->byArticle($id)->get();
}
public static function storeAttributes($article_id, $attributes)
{
foreach ($attributes as $key => $attribute)
{
self::storeAttribute($article_price_id, $attributes[$key]);
self::storeAttribute($article_id, $attributes[$key]);
}
}
public static function storeAttribute($article_price_id, $attribute)
public static function storeAttribute($article_id, $attribute)
{
$attribute['article_price_id'] = $article_price_id;
$attribute['article_id'] = $article_id;
unset($attribute['attribute_family_id']);
return self::store($attribute);
}

View File

@@ -13,9 +13,14 @@ use App\Models\Shop\ArticlePrice;
class ArticlePrices
{
public static function getPricesByArticle($id)
public static function getByArticle($id)
{
return ArticlePrice::with('ArticleAttributes.Value')->byArticle($id)->get()->toArray();
return ArticlePrice::byArticle($id)->get();
}
public static function getByArticleWithAttribute($id)
{
return ArticlePrice::with('article_attribute')->byArticle($id)->get();
}
public static function getDatatable()
@@ -34,11 +39,11 @@ class ArticlePrices
return ArticlePrice::find($id);
}
public static function storePrices($article_id, $prices)
public static function storePrices($article_attribute_id, $prices)
{
if ($prices) {
foreach ($prices as $key => $price) {
$prices[$key]['article_id'] = $article_id;
$prices[$key]['article_attribute_id'] = $article_attribute_id;
self::store($prices[$key]);
}
} else {

View File

@@ -30,6 +30,7 @@ class Articles
$data = $article->toArray();
$data['categories'] = self::getCategoriesByArticle($article);
$data['tags'] = self::getTagsByArticle($article);
// $data['attributes'] = self::getAttributesByArticle($article);
$data['prices'] = self::getPricesByArticle($article);
self::getMeta($data);
return $data;
@@ -56,9 +57,14 @@ class Articles
return $article->tags->pluck('id')->toArray();
}
public static function getAttributesByArticle($article)
{
return ArticleAttributes::getByArticleWithPrices($article->id)->toArray();
}
public static function getPricesByArticle($article)
{
return ArticlePrices::getPricesByArticle($article->id);
return ArticlePrices::getByArticleWithAttribute($article->id)->toArray();
}
public static function get($id)