Refactor Brand, Category, Product, and ModelPropValue models to remove morph relationships; update migration files for props and model prop values to reflect new foreign key structure.
This commit is contained in:
@@ -2,29 +2,10 @@
|
||||
|
||||
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
|
||||
{
|
||||
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,29 +2,10 @@
|
||||
|
||||
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
|
||||
{
|
||||
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,14 +2,11 @@
|
||||
|
||||
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',
|
||||
@@ -40,20 +37,4 @@ 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');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,12 +6,10 @@ 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',
|
||||
'prop_id',
|
||||
'value',
|
||||
])]
|
||||
class ModelPropValue extends Model
|
||||
@@ -20,19 +18,11 @@ class ModelPropValue extends Model
|
||||
|
||||
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');
|
||||
return $this->belongsTo(Prop::class, 'prop_id');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,16 +3,14 @@
|
||||
namespace App\Domains\Prop\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
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',
|
||||
@@ -23,11 +21,11 @@ class Prop extends Model
|
||||
use HasFactory;
|
||||
|
||||
/**
|
||||
* @return MorphTo<Model, $this>
|
||||
* @param Builder<self> $query
|
||||
*/
|
||||
public function propable(): MorphTo
|
||||
public function scopeForModel(Builder $query, Model|string $model): void
|
||||
{
|
||||
return $this->morphTo();
|
||||
$query->where('propable_type', is_string($model) ? $model : $model::class);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -51,6 +49,6 @@ class Prop extends Model
|
||||
*/
|
||||
public function modelValues(): HasMany
|
||||
{
|
||||
return $this->hasMany(ModelPropValue::class, 'prop_codigo', 'codigo');
|
||||
return $this->hasMany(ModelPropValue::class, 'prop_id');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,11 +6,11 @@ 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\Builder;
|
||||
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',
|
||||
@@ -40,18 +40,19 @@ class Tenant extends Model
|
||||
}
|
||||
|
||||
/**
|
||||
* @return MorphMany<Prop, $this>
|
||||
* @return Builder<Prop>
|
||||
*/
|
||||
public function props(): MorphMany
|
||||
public function props(): Builder
|
||||
{
|
||||
return $this->morphMany(Prop::class, 'propable');
|
||||
return Prop::query()->forModel(static::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return MorphMany<ModelPropValue, $this>
|
||||
* @return HasMany<ModelPropValue, $this>
|
||||
*/
|
||||
public function propValues(): MorphMany
|
||||
public function propValues(): HasMany
|
||||
{
|
||||
return $this->morphMany(ModelPropValue::class, 'valuable');
|
||||
return $this->hasMany(ModelPropValue::class, 'valuable_id')
|
||||
->whereHas('prop', fn (Builder $query) => $query->forModel(static::class));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,15 +14,13 @@ return new class extends Migration
|
||||
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']);
|
||||
$table->index('propable_type');
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -13,20 +13,13 @@ return new class extends Migration
|
||||
{
|
||||
Schema::create('model_prop_values', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->string('valuable_type');
|
||||
$table->unsignedBigInteger('valuable_id');
|
||||
$table->string('prop_codigo');
|
||||
$table->foreignId('prop_id')->constrained('props')->cascadeOnUpdate()->cascadeOnDelete();
|
||||
$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']);
|
||||
$table->unique(['prop_id', 'valuable_id']);
|
||||
$table->index('valuable_id');
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user