first upload all files
This commit is contained in:
112
Modules/Attribute/Entities/Attribute.php
Normal file
112
Modules/Attribute/Entities/Attribute.php
Normal file
@@ -0,0 +1,112 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Attribute\Entities;
|
||||
|
||||
use Modules\Support\Eloquent\Model;
|
||||
use Modules\Category\Entities\Category;
|
||||
use Modules\Support\Eloquent\Sluggable;
|
||||
use Modules\Support\Eloquent\Translatable;
|
||||
use Modules\Attribute\Admin\AttributeTable;
|
||||
|
||||
class Attribute extends Model
|
||||
{
|
||||
use Translatable, Sluggable;
|
||||
|
||||
/**
|
||||
* The relations to eager load on every query.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $with = ['translations'];
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $fillable = ['attribute_set_id', 'slug', 'is_filterable'];
|
||||
|
||||
/**
|
||||
* The attributes that should be cast to native types.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $casts = [
|
||||
'is_filterable' => 'boolean',
|
||||
];
|
||||
|
||||
/**
|
||||
* The attributes that are translatable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $translatedAttributes = ['name'];
|
||||
|
||||
/**
|
||||
* The attribute that will be slugged.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $slugAttribute = 'name';
|
||||
|
||||
/**
|
||||
* Perform any actions required after the model boots.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected static function booted()
|
||||
{
|
||||
static::saved(function (self $attribute) {
|
||||
$attribute->saveRelations(request()->all());
|
||||
});
|
||||
}
|
||||
|
||||
public function attributeSet()
|
||||
{
|
||||
return $this->belongsTo(AttributeSet::class);
|
||||
}
|
||||
|
||||
public function categories()
|
||||
{
|
||||
return $this->belongsToMany(Category::class, 'attribute_categories');
|
||||
}
|
||||
|
||||
public function values()
|
||||
{
|
||||
return $this->hasMany(AttributeValue::class)->orderBy('position');
|
||||
}
|
||||
|
||||
public function table()
|
||||
{
|
||||
return new AttributeTable($this->with('attributeSet'));
|
||||
}
|
||||
|
||||
public function saveRelations(array $attributes)
|
||||
{
|
||||
$this->categories()->sync(array_get($attributes, 'categories', []));
|
||||
$this->saveValues(array_get($attributes, 'values', []));
|
||||
}
|
||||
|
||||
public function saveValues($values = [])
|
||||
{
|
||||
$ids = $this->getDeleteCandidates($values);
|
||||
|
||||
if ($ids->isNotEmpty()) {
|
||||
$this->values()->whereIn('id', $ids)->delete();
|
||||
}
|
||||
|
||||
foreach (array_reset_index($values) as $index => $value) {
|
||||
$this->values()->updateOrCreate(
|
||||
['id' => $value['id']],
|
||||
$value + ['position' => $index]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private function getDeleteCandidates($values = [])
|
||||
{
|
||||
return $this->values()
|
||||
->pluck('id')
|
||||
->diff(array_pluck($values, 'id'));
|
||||
}
|
||||
}
|
||||
43
Modules/Attribute/Entities/AttributeSet.php
Normal file
43
Modules/Attribute/Entities/AttributeSet.php
Normal file
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Attribute\Entities;
|
||||
|
||||
use Modules\Admin\Ui\AdminTable;
|
||||
use Modules\Support\Eloquent\Model;
|
||||
use Modules\Support\Eloquent\Translatable;
|
||||
|
||||
class AttributeSet extends Model
|
||||
{
|
||||
use Translatable;
|
||||
|
||||
/**
|
||||
* The relations to eager load on every query.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $with = ['translations'];
|
||||
|
||||
/**
|
||||
* The attributes that aren't mass assignable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $guarded = [];
|
||||
|
||||
/**
|
||||
* The attributes that are translatable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $translatedAttributes = ['name'];
|
||||
|
||||
public function attributes()
|
||||
{
|
||||
return $this->hasMany(Attribute::class);
|
||||
}
|
||||
|
||||
public function table()
|
||||
{
|
||||
return new AdminTable($this->newQuery());
|
||||
}
|
||||
}
|
||||
15
Modules/Attribute/Entities/AttributeSetTranslation.php
Normal file
15
Modules/Attribute/Entities/AttributeSetTranslation.php
Normal file
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Attribute\Entities;
|
||||
|
||||
use Modules\Support\Eloquent\TranslationModel;
|
||||
|
||||
class AttributeSetTranslation extends TranslationModel
|
||||
{
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $fillable = ['name'];
|
||||
}
|
||||
15
Modules/Attribute/Entities/AttributeTranslation.php
Normal file
15
Modules/Attribute/Entities/AttributeTranslation.php
Normal file
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Attribute\Entities;
|
||||
|
||||
use Modules\Support\Eloquent\TranslationModel;
|
||||
|
||||
class AttributeTranslation extends TranslationModel
|
||||
{
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $fillable = ['name'];
|
||||
}
|
||||
32
Modules/Attribute/Entities/AttributeValue.php
Normal file
32
Modules/Attribute/Entities/AttributeValue.php
Normal file
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Attribute\Entities;
|
||||
|
||||
use Modules\Support\Eloquent\Model;
|
||||
use Modules\Support\Eloquent\Translatable;
|
||||
|
||||
class AttributeValue extends Model
|
||||
{
|
||||
use Translatable;
|
||||
|
||||
/**
|
||||
* The relations to eager load on every query.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $with = ['translations'];
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $fillable = ['position'];
|
||||
|
||||
/**
|
||||
* The attributes that are translatable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $translatedAttributes = ['value'];
|
||||
}
|
||||
15
Modules/Attribute/Entities/AttributeValueTranslation.php
Normal file
15
Modules/Attribute/Entities/AttributeValueTranslation.php
Normal file
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Attribute\Entities;
|
||||
|
||||
use Modules\Support\Eloquent\TranslationModel;
|
||||
|
||||
class AttributeValueTranslation extends TranslationModel
|
||||
{
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $fillable = ['value'];
|
||||
}
|
||||
56
Modules/Attribute/Entities/ProductAttribute.php
Normal file
56
Modules/Attribute/Entities/ProductAttribute.php
Normal file
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Attribute\Entities;
|
||||
|
||||
use Modules\Support\Eloquent\Model;
|
||||
|
||||
class ProductAttribute extends Model
|
||||
{
|
||||
/**
|
||||
* The relations to eager load on every query.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $with = ['attribute', 'values'];
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $fillable = ['attribute_id'];
|
||||
|
||||
/**
|
||||
* The accessors to append to the model's array form.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $appends = ['name'];
|
||||
|
||||
/**
|
||||
* Indicates if the model should be timestamped.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
public $timestamps = false;
|
||||
|
||||
public function attribute()
|
||||
{
|
||||
return $this->belongsTo(Attribute::class);
|
||||
}
|
||||
|
||||
public function values()
|
||||
{
|
||||
return $this->hasMany(ProductAttributeValue::class, 'product_attribute_id');
|
||||
}
|
||||
|
||||
public function getNameAttribute()
|
||||
{
|
||||
return $this->attribute->name;
|
||||
}
|
||||
|
||||
public function getAttributeSetAttribute()
|
||||
{
|
||||
return $this->attribute->attributeSet->name;
|
||||
}
|
||||
}
|
||||
49
Modules/Attribute/Entities/ProductAttributeValue.php
Normal file
49
Modules/Attribute/Entities/ProductAttributeValue.php
Normal file
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Attribute\Entities;
|
||||
|
||||
use Modules\Support\Eloquent\Model;
|
||||
|
||||
class ProductAttributeValue extends Model
|
||||
{
|
||||
/**
|
||||
* The relations to eager load on every query.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $with = ['attributeValue'];
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $fillable = ['product_attribute_id', 'attribute_id'];
|
||||
|
||||
/**
|
||||
* The accessors to append to the model's array form.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $appends = ['value'];
|
||||
|
||||
public function exists()
|
||||
{
|
||||
return ! is_null($this->attributeValue);
|
||||
}
|
||||
|
||||
public function attributeValue()
|
||||
{
|
||||
return $this->belongsTo(AttributeValue::class, 'attribute_value_id');
|
||||
}
|
||||
|
||||
public function getIdAttribute()
|
||||
{
|
||||
return $this->attributeValue->id;
|
||||
}
|
||||
|
||||
public function getValueAttribute()
|
||||
{
|
||||
return $this->attributeValue->value;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user