Nesting form fields depending on relationships
I am not sure it is possible or not but I want to give it a try.
Let me give you some idea on my project:
Models:
So each Specification has SpecType and SpecCategory.
Now the product has specification like: Samsung S24 Ultra has 16GB ram and I want to structure the form in that way.
Let me give you some idea on my project:
Models:
// Product
class Product extends Model
{
public function brand(): BelongsTo
{
return $this->belongsTo(Brand::class);
}
public function specifications(): BelongsToMany
{
return $this->belongsToMany(Specification::class, 'product_specifications', 'product_id', 'specification_id');
}
}
// Specification
class Specification extends Model
{
public function category(): BelongsTo
{
return $this->belongsTo(SpecCategory::class, 'spec_category_id');
}
public function type(): BelongsTo
{
return $this->belongsTo(SpecType::class, 'spec_type_id');
}
public function products(): BelongsToMany
{
return $this->belongsToMany(Product::class);
}
}
// SpecCategory
class SpecCategory extends Model
{
public function types(): HasMany
{
return $this->hasMany(SpecType::class, 'spec_category_id');
}
public function specifications(): HasMany
{
return $this->hasMany(Specification::class, 'spec_category_id');
}
}
// SpecType
class SpecType extends Model
{
public function category(): BelongsTo
{
return $this->belongsTo(SpecCategory::class, 'spec_category_id');
}
public function specifications(): HasMany
{
return $this->hasMany(Specification::class, 'spec_type_id');
}
}// Product
class Product extends Model
{
public function brand(): BelongsTo
{
return $this->belongsTo(Brand::class);
}
public function specifications(): BelongsToMany
{
return $this->belongsToMany(Specification::class, 'product_specifications', 'product_id', 'specification_id');
}
}
// Specification
class Specification extends Model
{
public function category(): BelongsTo
{
return $this->belongsTo(SpecCategory::class, 'spec_category_id');
}
public function type(): BelongsTo
{
return $this->belongsTo(SpecType::class, 'spec_type_id');
}
public function products(): BelongsToMany
{
return $this->belongsToMany(Product::class);
}
}
// SpecCategory
class SpecCategory extends Model
{
public function types(): HasMany
{
return $this->hasMany(SpecType::class, 'spec_category_id');
}
public function specifications(): HasMany
{
return $this->hasMany(Specification::class, 'spec_category_id');
}
}
// SpecType
class SpecType extends Model
{
public function category(): BelongsTo
{
return $this->belongsTo(SpecCategory::class, 'spec_category_id');
}
public function specifications(): HasMany
{
return $this->hasMany(Specification::class, 'spec_type_id');
}
}So each Specification has SpecType and SpecCategory.
example:example: specification: 16GB, type: RAM, category: MemoryNow the product has specification like: Samsung S24 Ultra has 16GB ram and I want to structure the form in that way.




