Laravel: количество KeyProduct в зависимости от количества с использованием faker - PullRequest
1 голос
/ 27 мая 2020

Я пытаюсь добавить ключ с помощью фейкера для каждого продукта, который у меня есть. Я не могу перенести свои таблицы, я получаю сообщение об ошибке General error: 1215 Cannot add foreign key constraint (SQL: alter table `key_products` add constraint `key_products_activation_key_foreign` foreign key (`activation_key`) references `products` (`activation_key`) on delete set null on update cascade)

Product_table. php

Schema::create('products', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->float('price');
(...)
            $table->string('activation_key')->nullable();
            $table->timestamps();
        });

Create_key_products_table. php

 Schema::create('key_products', function (Blueprint $table) {

            $table->increments('id');
            $table->string('activation_key');

            $table->foreign('activation_key')->references('activation_key')
            ->on('products')->onUpdate('cascade')->onDelete('set null');

            $table->timestamps();
        });

ProductFactory. php

$factory->define(Product::class, function (Faker $faker) {
    return [
        'activation_key' => $faker->uuid
    ];
});

KeyProductFactory. php

$factory->define(KeyProduct::class, function (Faker $faker) {

    $product = factory(Product::class)->create();
    return [
            'activation_key' => $product->activation_key,

    ];
});

CategoryFactory. php

$factory->define(Category::class, function (Faker $faker) {
    return [
        'activation_key' => $faker->uuid
    ];
});

ProductController

public function create()
    {
        $categories = Category::all();
        return view('products.create', ['categories' => $categories]);

        $faker = Faker::create();
        for ($index = 0; $index < $inputs["quantity"]; $index++) {
            $gameActivationKey = new Game_activation_key();
            $gameActivationKey->category_id = $gamePlatform->id;
            $gameActivationKey->activation_key = $faker->uuid;
            $gameActivationKey->save();
        }
    }
...