Factory yapısını ilk defa kullanıcam ve kullanmam gereken yer biraz karışık olduğu için kafam karıştı 😀
Bu; "renk", "boy" gibi anahtar kelimeleri içeren tablom
Schema::create('product_property_names', function (Blueprint $table) {
$table->id();
$table->string("name",60)->unique();
$table->timestamps();
});
Bu da mavi, yeşil, sarı veya 5inç 4inç gibi değerler içeren tablom. Her değer, yukarıdaki tablodaki bir veriye yani o değerin sahip olduğu özelliğin ismine bağlı.
Schema::create('product_property_values', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('property_name_id');
$table->string('value',60);
$table->timestamps();
$table->foreign('property_name_id')
->references('id')
->on('product_property_names');
$table->unique(['property_name_id', 'value']);
});
Son olarak bu da varyantlar tablom. İlgili özelliğin anahtarını, değerini ve o varyantın stoğu ve fiyatını tutuyor
Schema::create('product_variants', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('product_id');
$table->unsignedBigInteger('property_name_id');
$table->unsignedBigInteger('property_value_id');
$table->unsignedDecimal('price', 9, 2);
$table->unsignedInteger('stock')->default(0);
$table->boolean('is_price_using')->default(0);
$table->timestamps();
$table->unique(['product_id', 'property_name_id']);
$table->foreign('product_id')
->references('id')
->on('products')
->onDelete('cascade');
$table->foreign('property_name_id')
->references('id')
->on('product_property_names');
$table->foreign('property_value_id')
->references('id')
->on('product_property_values');
});
Ürün özellik adları ve özellik değerlerini bir seederın içinde kendim elle hazırladım.
Seederım:
public function run()
{
$properties = [
'color' => ['red', 'blue', 'yellow', 'brown', 'green', 'orange', 'white'],
'body' => ['medium', 'small', 'large', 'xsmall', 'xlarge'],
'screen' => ['5inch', '4inch', '3inch', '4.4inch']
];
foreach ($properties as $propertyName => $propertyArray) {
$propertyNameModel = ProductPropertyName::create(['name' => $propertyName]);
foreach ($propertyArray as $property) {
ProductPropertyValue::create([
'property_name_id' => $propertyNameModel->id,
'value' => $property
]);
}
}
}
Seederda oluşturduğum bu özellikleri kullanarak varyantı olan ürünler üreten bir factory oluşturmak istiyorum nasıl yaparım?