Add Prop and ModelPropValue models with relationships; create migration files for props, prop options, prop data types, and model prop values
This commit is contained in:
@@ -2,9 +2,29 @@
|
||||
|
||||
namespace App\Domains\Product\Models;
|
||||
|
||||
use App\Domains\Prop\Models\ModelPropValue;
|
||||
use App\Domains\Prop\Models\Prop;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\MorphMany;
|
||||
|
||||
class Brand extends Model
|
||||
{
|
||||
// Placeholder model for the product domain structure.
|
||||
use HasFactory;
|
||||
|
||||
/**
|
||||
* @return MorphMany<Prop, $this>
|
||||
*/
|
||||
public function props(): MorphMany
|
||||
{
|
||||
return $this->morphMany(Prop::class, 'propable');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return MorphMany<ModelPropValue, $this>
|
||||
*/
|
||||
public function propValues(): MorphMany
|
||||
{
|
||||
return $this->morphMany(ModelPropValue::class, 'valuable');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,9 +2,29 @@
|
||||
|
||||
namespace App\Domains\Product\Models;
|
||||
|
||||
use App\Domains\Prop\Models\ModelPropValue;
|
||||
use App\Domains\Prop\Models\Prop;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\MorphMany;
|
||||
|
||||
class Category extends Model
|
||||
{
|
||||
// Placeholder model for the product domain structure.
|
||||
use HasFactory;
|
||||
|
||||
/**
|
||||
* @return MorphMany<Prop, $this>
|
||||
*/
|
||||
public function props(): MorphMany
|
||||
{
|
||||
return $this->morphMany(Prop::class, 'propable');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return MorphMany<ModelPropValue, $this>
|
||||
*/
|
||||
public function propValues(): MorphMany
|
||||
{
|
||||
return $this->morphMany(ModelPropValue::class, 'valuable');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,11 +2,14 @@
|
||||
|
||||
namespace App\Domains\Product\Models;
|
||||
|
||||
use App\Domains\Prop\Models\ModelPropValue;
|
||||
use App\Domains\Prop\Models\Prop;
|
||||
use App\Domains\Tenant\Models\Tenant;
|
||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\MorphMany;
|
||||
|
||||
#[Fillable([
|
||||
'tenant_codigo',
|
||||
@@ -37,4 +40,20 @@ class Product extends Model
|
||||
{
|
||||
return $this->belongsTo(Tenant::class, 'tenant_codigo', 'codigo');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return MorphMany<Prop, $this>
|
||||
*/
|
||||
public function props(): MorphMany
|
||||
{
|
||||
return $this->morphMany(Prop::class, 'propable');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return MorphMany<ModelPropValue, $this>
|
||||
*/
|
||||
public function propValues(): MorphMany
|
||||
{
|
||||
return $this->morphMany(ModelPropValue::class, 'valuable');
|
||||
}
|
||||
}
|
||||
|
||||
38
app/Domains/Prop/Models/ModelPropValue.php
Normal file
38
app/Domains/Prop/Models/ModelPropValue.php
Normal file
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domains\Prop\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\MorphTo;
|
||||
|
||||
#[Fillable([
|
||||
'valuable_type',
|
||||
'valuable_id',
|
||||
'prop_codigo',
|
||||
'value',
|
||||
])]
|
||||
class ModelPropValue extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
protected $table = 'model_prop_values';
|
||||
|
||||
/**
|
||||
* @return MorphTo<Model, $this>
|
||||
*/
|
||||
public function valuable(): MorphTo
|
||||
{
|
||||
return $this->morphTo();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return BelongsTo<Prop, $this>
|
||||
*/
|
||||
public function prop(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Prop::class, 'prop_codigo', 'codigo');
|
||||
}
|
||||
}
|
||||
56
app/Domains/Prop/Models/Prop.php
Normal file
56
app/Domains/Prop/Models/Prop.php
Normal file
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domains\Prop\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Database\Eloquent\Relations\MorphTo;
|
||||
|
||||
#[Fillable([
|
||||
'propable_type',
|
||||
'propable_id',
|
||||
'scope',
|
||||
'codigo',
|
||||
'nombre',
|
||||
'is_required',
|
||||
'data_type_id',
|
||||
])]
|
||||
class Prop extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
/**
|
||||
* @return MorphTo<Model, $this>
|
||||
*/
|
||||
public function propable(): MorphTo
|
||||
{
|
||||
return $this->morphTo();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return BelongsTo<PropDataType, $this>
|
||||
*/
|
||||
public function dataType(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(PropDataType::class, 'data_type_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return HasMany<PropOption, $this>
|
||||
*/
|
||||
public function options(): HasMany
|
||||
{
|
||||
return $this->hasMany(PropOption::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return HasMany<ModelPropValue, $this>
|
||||
*/
|
||||
public function modelValues(): HasMany
|
||||
{
|
||||
return $this->hasMany(ModelPropValue::class, 'prop_codigo', 'codigo');
|
||||
}
|
||||
}
|
||||
25
app/Domains/Prop/Models/PropDataType.php
Normal file
25
app/Domains/Prop/Models/PropDataType.php
Normal file
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domains\Prop\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
|
||||
#[Fillable([
|
||||
'name',
|
||||
'code',
|
||||
])]
|
||||
class PropDataType extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
/**
|
||||
* @return HasMany<Prop, $this>
|
||||
*/
|
||||
public function props(): HasMany
|
||||
{
|
||||
return $this->hasMany(Prop::class, 'data_type_id');
|
||||
}
|
||||
}
|
||||
27
app/Domains/Prop/Models/PropOption.php
Normal file
27
app/Domains/Prop/Models/PropOption.php
Normal file
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domains\Prop\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
#[Fillable([
|
||||
'prop_id',
|
||||
'value',
|
||||
'label',
|
||||
'sort_order',
|
||||
])]
|
||||
class PropOption extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
/**
|
||||
* @return BelongsTo<Prop, $this>
|
||||
*/
|
||||
public function prop(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Prop::class);
|
||||
}
|
||||
}
|
||||
@@ -2,12 +2,15 @@
|
||||
|
||||
namespace App\Domains\Tenant\Models;
|
||||
|
||||
use App\Domains\Prop\Models\ModelPropValue;
|
||||
use App\Domains\Prop\Models\Prop;
|
||||
use App\Domains\Product\Models\Product;
|
||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Database\Eloquent\Relations\MorphMany;
|
||||
|
||||
#[Fillable([
|
||||
'codigo',
|
||||
@@ -35,4 +38,20 @@ class Tenant extends Model
|
||||
->withPivot('valor')
|
||||
->withTimestamps();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return MorphMany<Prop, $this>
|
||||
*/
|
||||
public function props(): MorphMany
|
||||
{
|
||||
return $this->morphMany(Prop::class, 'propable');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return MorphMany<ModelPropValue, $this>
|
||||
*/
|
||||
public function propValues(): MorphMany
|
||||
{
|
||||
return $this->morphMany(ModelPropValue::class, 'valuable');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
Schema::create('props_data_types', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->string('name');
|
||||
$table->string('code')->unique();
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('props_data_types');
|
||||
}
|
||||
};
|
||||
36
database/migrations/2026_06_22_120100_create_props_table.php
Normal file
36
database/migrations/2026_06_22_120100_create_props_table.php
Normal file
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
Schema::create('props', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->string('propable_type');
|
||||
$table->unsignedBigInteger('propable_id')->nullable();
|
||||
$table->enum('scope', ['global', 'particular']);
|
||||
$table->string('codigo')->unique();
|
||||
$table->string('nombre');
|
||||
$table->boolean('is_required')->default(false);
|
||||
$table->foreignId('data_type_id')->constrained('props_data_types')->cascadeOnUpdate()->restrictOnDelete();
|
||||
$table->timestamps();
|
||||
|
||||
$table->index(['propable_type', 'propable_id']);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('props');
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
Schema::create('prop_options', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->foreignId('prop_id')->constrained('props')->cascadeOnUpdate()->cascadeOnDelete();
|
||||
$table->string('value');
|
||||
$table->string('label');
|
||||
$table->unsignedInteger('sort_order')->default(0);
|
||||
$table->timestamps();
|
||||
|
||||
$table->unique(['prop_id', 'value']);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('prop_options');
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
Schema::create('model_prop_values', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->string('valuable_type');
|
||||
$table->unsignedBigInteger('valuable_id');
|
||||
$table->string('prop_codigo');
|
||||
$table->text('value')->nullable();
|
||||
$table->timestamps();
|
||||
|
||||
$table->foreign('prop_codigo')
|
||||
->references('codigo')
|
||||
->on('props')
|
||||
->cascadeOnUpdate()
|
||||
->cascadeOnDelete();
|
||||
|
||||
$table->unique(['valuable_type', 'valuable_id', 'prop_codigo']);
|
||||
$table->index(['valuable_type', 'valuable_id']);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('model_prop_values');
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user