reupload
This commit is contained in:
103
app/Models/Ecommerce/Product/Property/Attribute.php
Normal file
103
app/Models/Ecommerce/Product/Property/Attribute.php
Normal file
@@ -0,0 +1,103 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\Ecommerce\Product\Property;
|
||||
|
||||
use App\Models\Ecommerce\Product\Category\Category;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Spatie\EloquentSortable\Sortable;
|
||||
use Spatie\EloquentSortable\SortableTrait;
|
||||
use Spatie\Sluggable\HasSlug;
|
||||
use Spatie\Sluggable\SlugOptions;
|
||||
use Spatie\Translatable\HasTranslations;
|
||||
|
||||
class Attribute extends Model implements Sortable
|
||||
{
|
||||
use HasFactory;
|
||||
use HasSlug;
|
||||
use HasTranslations;
|
||||
use SortableTrait;
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*/
|
||||
protected $fillable = [
|
||||
'name',
|
||||
'slug',
|
||||
'description',
|
||||
'type',
|
||||
'is_visible',
|
||||
'is_required',
|
||||
'is_searchable',
|
||||
'is_filterable',
|
||||
'sort_order',
|
||||
];
|
||||
|
||||
/**
|
||||
* Translatable fields
|
||||
*
|
||||
* @var array<string>
|
||||
*/
|
||||
public $translatable = ['name', 'description'];
|
||||
|
||||
/**
|
||||
* Translatable fields
|
||||
*
|
||||
* @var array<string, mixed>
|
||||
*/
|
||||
public $sortable = [
|
||||
'order_column_name' => 'sort_order',
|
||||
'sort_when_creating' => true,
|
||||
];
|
||||
|
||||
/**
|
||||
* Get the options for generating the slug.
|
||||
*/
|
||||
public function getSlugOptions(): SlugOptions
|
||||
{
|
||||
return SlugOptions::create()
|
||||
->generateSlugsFrom('name')
|
||||
->saveSlugsTo('slug');
|
||||
}
|
||||
|
||||
/**
|
||||
* Attribute values (color: red, black, ...)
|
||||
*/
|
||||
public function values(): HasMany
|
||||
{
|
||||
return $this->hasMany(AttributeValue::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Attribute Category
|
||||
*/
|
||||
public function category(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Category::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Attributes categories as belongs-to-many
|
||||
*/
|
||||
public function categories(): BelongsToMany
|
||||
{
|
||||
return $this->belongsToMany(Category::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return available fields types.
|
||||
*
|
||||
* @return array<string, string>
|
||||
*/
|
||||
public static function typesFields(): array
|
||||
{
|
||||
return [
|
||||
'text' => __('Text'),
|
||||
'number' => __('Number'),
|
||||
'select' => __('Select option'),
|
||||
];
|
||||
}
|
||||
}
|
||||
65
app/Models/Ecommerce/Product/Property/AttributeValue.php
Normal file
65
app/Models/Ecommerce/Product/Property/AttributeValue.php
Normal file
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\Ecommerce\Product\Property;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Spatie\EloquentSortable\Sortable;
|
||||
use Spatie\EloquentSortable\SortableTrait;
|
||||
use Spatie\Sluggable\HasSlug;
|
||||
use Spatie\Sluggable\SlugOptions;
|
||||
use Spatie\Translatable\HasTranslations;
|
||||
|
||||
class AttributeValue extends Model implements Sortable
|
||||
{
|
||||
use HasFactory;
|
||||
use HasSlug;
|
||||
use HasTranslations;
|
||||
use SortableTrait;
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*/
|
||||
protected $fillable = [
|
||||
'value',
|
||||
'key',
|
||||
'attribute_id',
|
||||
];
|
||||
|
||||
/**
|
||||
* Translatable fields
|
||||
*
|
||||
* @var array<string>
|
||||
*/
|
||||
public $translatable = ['value'];
|
||||
|
||||
/**
|
||||
* Translatable fields
|
||||
*
|
||||
* @var array<string, mixed>
|
||||
*/
|
||||
public $sortable = [
|
||||
'order_column_name' => 'sort_order',
|
||||
'sort_when_creating' => true,
|
||||
];
|
||||
|
||||
/**
|
||||
* Get the options for generating the slug.
|
||||
*/
|
||||
public function getSlugOptions(): SlugOptions
|
||||
{
|
||||
return SlugOptions::create()
|
||||
->generateSlugsFrom('name')
|
||||
->usingSeparator('_')
|
||||
->saveSlugsTo('key');
|
||||
}
|
||||
|
||||
/**
|
||||
* Attribute
|
||||
*/
|
||||
public function attribute(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Attribute::class, 'attribute_id');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\Ecommerce\Product\Property;
|
||||
|
||||
use Illuminate\Database\Eloquent\Casts\Attribute;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
class ProductAttributeValue extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
/**
|
||||
* Table name
|
||||
*/
|
||||
protected $table = 'attribute_value_product_attribute';
|
||||
|
||||
/**
|
||||
* Indicates if the model should be timestamped.
|
||||
*/
|
||||
public $timestamps = false;
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*
|
||||
* @var array<string>
|
||||
*/
|
||||
protected $fillable = [
|
||||
'product_attribute_id',
|
||||
'attribute_value_id',
|
||||
'product_custom_value',
|
||||
];
|
||||
|
||||
/**
|
||||
* The relations to eager load on every query.
|
||||
*/
|
||||
protected $with = ['value'];
|
||||
|
||||
/**
|
||||
* The accessors to append to the model's array form.
|
||||
*/
|
||||
protected $appends = ['real_value'];
|
||||
|
||||
/**
|
||||
* Return exact product attribute value.
|
||||
*
|
||||
* @return string|int
|
||||
*/
|
||||
public function realValue(): Attribute
|
||||
{
|
||||
return Attribute::make(
|
||||
get: fn () => $this->product_custom_value ?: ($this->value?->value ?? '')
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Attribute Value for forms
|
||||
*/
|
||||
public function valueForForms(): null|int|string
|
||||
{
|
||||
return $this->product_custom_value ?: $this->attribute_value_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Value
|
||||
*/
|
||||
public function value(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(AttributeValue::class, 'attribute_value_id');
|
||||
}
|
||||
}
|
||||
65
app/Models/Ecommerce/Product/Property/ProductProperty.php
Normal file
65
app/Models/Ecommerce/Product/Property/ProductProperty.php
Normal file
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\Ecommerce\Product\Property;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
|
||||
class ProductProperty extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
/**
|
||||
* The table associated with the model.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $table = 'product_attributes';
|
||||
|
||||
/**
|
||||
* Indicates if the model should be timestamped.
|
||||
*/
|
||||
public $timestamps = false;
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*/
|
||||
protected $fillable = [
|
||||
'product_id',
|
||||
'attribute_id',
|
||||
];
|
||||
|
||||
/**
|
||||
* The relations to eager load on every query.
|
||||
*/
|
||||
protected $with = [
|
||||
'attribute',
|
||||
'values',
|
||||
];
|
||||
|
||||
/**
|
||||
* Attribute
|
||||
*/
|
||||
public function attribute(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Attribute::class, 'attribute_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* Product
|
||||
*/
|
||||
public function product(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Product::class, 'product_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* Property values
|
||||
*/
|
||||
public function values(): HasMany
|
||||
{
|
||||
return $this->hasMany(ProductAttributeValue::class, 'product_attribute_id');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user