Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Database 需要重置计数器的一对多的laravel数据库种子设定_Database_Laravel_One To Many_Seeding - Fatal编程技术网

Database 需要重置计数器的一对多的laravel数据库种子设定

Database 需要重置计数器的一对多的laravel数据库种子设定,database,laravel,one-to-many,seeding,Database,Laravel,One To Many,Seeding,我正在为配方应用程序创建api,但在生成种子数据时遇到问题。配方数据库有4个for表:用户、配方、配方配料和配方步骤 用户有很多食谱。 配方属于用户。 配方有很多成分(1到n)。 配方有许多步骤(1到n) 到目前为止,种子生成代码似乎可以为每个配方随机生成5到10种成分。生成步骤数据时,每个配方的步骤顺序都会不断增加,但不是我期望的方式。我需要每个配方的步骤顺序从1开始 我使用内置的php服务器在本地Ubuntu18.04桌面上运行这个程序 以下是我为表迁移准备的内容: 创建配方表: if (!

我正在为配方应用程序创建api,但在生成种子数据时遇到问题。配方数据库有4个for表:用户、配方、配方配料和配方步骤

用户有很多食谱。 配方属于用户。 配方有很多成分(1到n)。 配方有许多步骤(1到n)

到目前为止,种子生成代码似乎可以为每个配方随机生成5到10种成分。生成步骤数据时,每个配方的步骤顺序都会不断增加,但不是我期望的方式。我需要每个配方的步骤顺序从1开始

我使用内置的php服务器在本地Ubuntu18.04桌面上运行这个程序

以下是我为表迁移准备的内容:

创建配方表:

if (!Schema::hasTable('recipes')) {
   Schema::create('recipes', static function (Blueprint $table) {
     $table->bigIncrements('id');
     $table->unsignedBigInteger('user_id')->nullable();
     $table->foreign('user_id')->references('id')->on('users');
     $table->string('title', 256);
     $table->longText('description');
     $table->timestamps();
   });
}
创建配方配料表:

if (!Schema::hasTable('recipe_ingredients')) {
   Schema::create('recipe_ingredients', static function (Blueprint $table) {
     $table->unsignedBigInteger('recipe_id');
     $table->string('name', 100);
     $table->string('quantity', '15');
     $table->timestamps();
     $table->foreign('recipe_id')->references('id')->on('recipes')->onDelete('cascade');
    });
}
if (!Schema::hasTable('recipe_steps')) {
   Schema::create('recipe_steps', static function (Blueprint $table){
      $table->unsignedBigInteger('recipe_id');
      $table->unsignedInteger('step_order');
      $table->text('description');
      $table->timestamps();
      $table->foreign('recipe_id')->references('id')->on('recipes')->onDelete('cascade');
   });
}
public function User(): \Illuminate\Database\Eloquent\Relations\BelongsTo
    {
        /** @var Recipe $this */
        return $this->belongsTo(User::class);
    }

    /**
     * @return HasMany
     */
    public function ingredients(): HasMany
    {
        return $this->hasMany(Ingredient::class);
    }

    public function steps(): HasMany
    {
        return $this->hasMany(Ingredient::class);
    }
public function Recipe(): \Illuminate\Database\Eloquent\Relations\BelongsTo
{
    /** @var Ingredient $this */
    return $this->belongsTo(Recipe::class);
}
public function Recipe(): BelongsTo
{
     /** @var Ingredient $this */
     return $this->belongsTo(Recipe::class);
}
$factory->define(Recipe::class, static function (Faker $faker) {
    return [
        'user_id' => factory(User::class)->create()->id,
        'title' => rtrim($faker->sentence(random_int(5, 10)), '.'),
        'description' => $faker->paragraphs(random_int(3, 7), true),
    ];
});
$factory->define(Ingredient::class, static function (Faker $faker) {
    $quantityTypeAmount = $faker->randomFloat(2, 0, 10);
    $quantityTypeString = $faker->randomElements(
        ['tsp.', 'tbsp.', 'fl oz.', 'gill.', 'cup.', 'pt.', 'qt.', 'gal.', 'ml.', 'L', 'dl',]
    );
    return [
        'name' => $faker->words(random_int(3, 7), true),
        'quantity' => (string)$quantityTypeAmount . ' ' . $quantityTypeString[0]
    ];
});
$factory->define(Step::class, static function (Faker $faker) {
    static $step_order = 1;
    return [
        'step_order' => $step_order++,
        'description' => $faker->sentences(random_int(1, 4), true),
    ];
});
// Create 5 users.
factory(User::class, 5)->create()->each(static function (User $user) {
    // Create 5 - 10 recipes for each user.
    factory(Recipe::class, 5)->create(['user_id'=>$user->id])->each(static function (Recipe $recipe) {

        // For each recipe, create 5 - 10 ingredients randomly.
        $recipe->ingredients()->saveMany(factory(Ingredient::class, random_int(3,7))->make());

        // For each recipe, create 1 - 5 steps for each recipe
        $recipe->steps()->saveMany(factory(Step::class,random_int(1,5))->make());

    });
});
创建配方步骤表:

if (!Schema::hasTable('recipe_ingredients')) {
   Schema::create('recipe_ingredients', static function (Blueprint $table) {
     $table->unsignedBigInteger('recipe_id');
     $table->string('name', 100);
     $table->string('quantity', '15');
     $table->timestamps();
     $table->foreign('recipe_id')->references('id')->on('recipes')->onDelete('cascade');
    });
}
if (!Schema::hasTable('recipe_steps')) {
   Schema::create('recipe_steps', static function (Blueprint $table){
      $table->unsignedBigInteger('recipe_id');
      $table->unsignedInteger('step_order');
      $table->text('description');
      $table->timestamps();
      $table->foreign('recipe_id')->references('id')->on('recipes')->onDelete('cascade');
   });
}
public function User(): \Illuminate\Database\Eloquent\Relations\BelongsTo
    {
        /** @var Recipe $this */
        return $this->belongsTo(User::class);
    }

    /**
     * @return HasMany
     */
    public function ingredients(): HasMany
    {
        return $this->hasMany(Ingredient::class);
    }

    public function steps(): HasMany
    {
        return $this->hasMany(Ingredient::class);
    }
public function Recipe(): \Illuminate\Database\Eloquent\Relations\BelongsTo
{
    /** @var Ingredient $this */
    return $this->belongsTo(Recipe::class);
}
public function Recipe(): BelongsTo
{
     /** @var Ingredient $this */
     return $this->belongsTo(Recipe::class);
}
$factory->define(Recipe::class, static function (Faker $faker) {
    return [
        'user_id' => factory(User::class)->create()->id,
        'title' => rtrim($faker->sentence(random_int(5, 10)), '.'),
        'description' => $faker->paragraphs(random_int(3, 7), true),
    ];
});
$factory->define(Ingredient::class, static function (Faker $faker) {
    $quantityTypeAmount = $faker->randomFloat(2, 0, 10);
    $quantityTypeString = $faker->randomElements(
        ['tsp.', 'tbsp.', 'fl oz.', 'gill.', 'cup.', 'pt.', 'qt.', 'gal.', 'ml.', 'L', 'dl',]
    );
    return [
        'name' => $faker->words(random_int(3, 7), true),
        'quantity' => (string)$quantityTypeAmount . ' ' . $quantityTypeString[0]
    ];
});
$factory->define(Step::class, static function (Faker $faker) {
    static $step_order = 1;
    return [
        'step_order' => $step_order++,
        'description' => $faker->sentences(random_int(1, 4), true),
    ];
});
// Create 5 users.
factory(User::class, 5)->create()->each(static function (User $user) {
    // Create 5 - 10 recipes for each user.
    factory(Recipe::class, 5)->create(['user_id'=>$user->id])->each(static function (Recipe $recipe) {

        // For each recipe, create 5 - 10 ingredients randomly.
        $recipe->ingredients()->saveMany(factory(Ingredient::class, random_int(3,7))->make());

        // For each recipe, create 1 - 5 steps for each recipe
        $recipe->steps()->saveMany(factory(Step::class,random_int(1,5))->make());

    });
});
配方型号:

if (!Schema::hasTable('recipe_ingredients')) {
   Schema::create('recipe_ingredients', static function (Blueprint $table) {
     $table->unsignedBigInteger('recipe_id');
     $table->string('name', 100);
     $table->string('quantity', '15');
     $table->timestamps();
     $table->foreign('recipe_id')->references('id')->on('recipes')->onDelete('cascade');
    });
}
if (!Schema::hasTable('recipe_steps')) {
   Schema::create('recipe_steps', static function (Blueprint $table){
      $table->unsignedBigInteger('recipe_id');
      $table->unsignedInteger('step_order');
      $table->text('description');
      $table->timestamps();
      $table->foreign('recipe_id')->references('id')->on('recipes')->onDelete('cascade');
   });
}
public function User(): \Illuminate\Database\Eloquent\Relations\BelongsTo
    {
        /** @var Recipe $this */
        return $this->belongsTo(User::class);
    }

    /**
     * @return HasMany
     */
    public function ingredients(): HasMany
    {
        return $this->hasMany(Ingredient::class);
    }

    public function steps(): HasMany
    {
        return $this->hasMany(Ingredient::class);
    }
public function Recipe(): \Illuminate\Database\Eloquent\Relations\BelongsTo
{
    /** @var Ingredient $this */
    return $this->belongsTo(Recipe::class);
}
public function Recipe(): BelongsTo
{
     /** @var Ingredient $this */
     return $this->belongsTo(Recipe::class);
}
$factory->define(Recipe::class, static function (Faker $faker) {
    return [
        'user_id' => factory(User::class)->create()->id,
        'title' => rtrim($faker->sentence(random_int(5, 10)), '.'),
        'description' => $faker->paragraphs(random_int(3, 7), true),
    ];
});
$factory->define(Ingredient::class, static function (Faker $faker) {
    $quantityTypeAmount = $faker->randomFloat(2, 0, 10);
    $quantityTypeString = $faker->randomElements(
        ['tsp.', 'tbsp.', 'fl oz.', 'gill.', 'cup.', 'pt.', 'qt.', 'gal.', 'ml.', 'L', 'dl',]
    );
    return [
        'name' => $faker->words(random_int(3, 7), true),
        'quantity' => (string)$quantityTypeAmount . ' ' . $quantityTypeString[0]
    ];
});
$factory->define(Step::class, static function (Faker $faker) {
    static $step_order = 1;
    return [
        'step_order' => $step_order++,
        'description' => $faker->sentences(random_int(1, 4), true),
    ];
});
// Create 5 users.
factory(User::class, 5)->create()->each(static function (User $user) {
    // Create 5 - 10 recipes for each user.
    factory(Recipe::class, 5)->create(['user_id'=>$user->id])->each(static function (Recipe $recipe) {

        // For each recipe, create 5 - 10 ingredients randomly.
        $recipe->ingredients()->saveMany(factory(Ingredient::class, random_int(3,7))->make());

        // For each recipe, create 1 - 5 steps for each recipe
        $recipe->steps()->saveMany(factory(Step::class,random_int(1,5))->make());

    });
});
配料型号:

if (!Schema::hasTable('recipe_ingredients')) {
   Schema::create('recipe_ingredients', static function (Blueprint $table) {
     $table->unsignedBigInteger('recipe_id');
     $table->string('name', 100);
     $table->string('quantity', '15');
     $table->timestamps();
     $table->foreign('recipe_id')->references('id')->on('recipes')->onDelete('cascade');
    });
}
if (!Schema::hasTable('recipe_steps')) {
   Schema::create('recipe_steps', static function (Blueprint $table){
      $table->unsignedBigInteger('recipe_id');
      $table->unsignedInteger('step_order');
      $table->text('description');
      $table->timestamps();
      $table->foreign('recipe_id')->references('id')->on('recipes')->onDelete('cascade');
   });
}
public function User(): \Illuminate\Database\Eloquent\Relations\BelongsTo
    {
        /** @var Recipe $this */
        return $this->belongsTo(User::class);
    }

    /**
     * @return HasMany
     */
    public function ingredients(): HasMany
    {
        return $this->hasMany(Ingredient::class);
    }

    public function steps(): HasMany
    {
        return $this->hasMany(Ingredient::class);
    }
public function Recipe(): \Illuminate\Database\Eloquent\Relations\BelongsTo
{
    /** @var Ingredient $this */
    return $this->belongsTo(Recipe::class);
}
public function Recipe(): BelongsTo
{
     /** @var Ingredient $this */
     return $this->belongsTo(Recipe::class);
}
$factory->define(Recipe::class, static function (Faker $faker) {
    return [
        'user_id' => factory(User::class)->create()->id,
        'title' => rtrim($faker->sentence(random_int(5, 10)), '.'),
        'description' => $faker->paragraphs(random_int(3, 7), true),
    ];
});
$factory->define(Ingredient::class, static function (Faker $faker) {
    $quantityTypeAmount = $faker->randomFloat(2, 0, 10);
    $quantityTypeString = $faker->randomElements(
        ['tsp.', 'tbsp.', 'fl oz.', 'gill.', 'cup.', 'pt.', 'qt.', 'gal.', 'ml.', 'L', 'dl',]
    );
    return [
        'name' => $faker->words(random_int(3, 7), true),
        'quantity' => (string)$quantityTypeAmount . ' ' . $quantityTypeString[0]
    ];
});
$factory->define(Step::class, static function (Faker $faker) {
    static $step_order = 1;
    return [
        'step_order' => $step_order++,
        'description' => $faker->sentences(random_int(1, 4), true),
    ];
});
// Create 5 users.
factory(User::class, 5)->create()->each(static function (User $user) {
    // Create 5 - 10 recipes for each user.
    factory(Recipe::class, 5)->create(['user_id'=>$user->id])->each(static function (Recipe $recipe) {

        // For each recipe, create 5 - 10 ingredients randomly.
        $recipe->ingredients()->saveMany(factory(Ingredient::class, random_int(3,7))->make());

        // For each recipe, create 1 - 5 steps for each recipe
        $recipe->steps()->saveMany(factory(Step::class,random_int(1,5))->make());

    });
});
步骤模型:

if (!Schema::hasTable('recipe_ingredients')) {
   Schema::create('recipe_ingredients', static function (Blueprint $table) {
     $table->unsignedBigInteger('recipe_id');
     $table->string('name', 100);
     $table->string('quantity', '15');
     $table->timestamps();
     $table->foreign('recipe_id')->references('id')->on('recipes')->onDelete('cascade');
    });
}
if (!Schema::hasTable('recipe_steps')) {
   Schema::create('recipe_steps', static function (Blueprint $table){
      $table->unsignedBigInteger('recipe_id');
      $table->unsignedInteger('step_order');
      $table->text('description');
      $table->timestamps();
      $table->foreign('recipe_id')->references('id')->on('recipes')->onDelete('cascade');
   });
}
public function User(): \Illuminate\Database\Eloquent\Relations\BelongsTo
    {
        /** @var Recipe $this */
        return $this->belongsTo(User::class);
    }

    /**
     * @return HasMany
     */
    public function ingredients(): HasMany
    {
        return $this->hasMany(Ingredient::class);
    }

    public function steps(): HasMany
    {
        return $this->hasMany(Ingredient::class);
    }
public function Recipe(): \Illuminate\Database\Eloquent\Relations\BelongsTo
{
    /** @var Ingredient $this */
    return $this->belongsTo(Recipe::class);
}
public function Recipe(): BelongsTo
{
     /** @var Ingredient $this */
     return $this->belongsTo(Recipe::class);
}
$factory->define(Recipe::class, static function (Faker $faker) {
    return [
        'user_id' => factory(User::class)->create()->id,
        'title' => rtrim($faker->sentence(random_int(5, 10)), '.'),
        'description' => $faker->paragraphs(random_int(3, 7), true),
    ];
});
$factory->define(Ingredient::class, static function (Faker $faker) {
    $quantityTypeAmount = $faker->randomFloat(2, 0, 10);
    $quantityTypeString = $faker->randomElements(
        ['tsp.', 'tbsp.', 'fl oz.', 'gill.', 'cup.', 'pt.', 'qt.', 'gal.', 'ml.', 'L', 'dl',]
    );
    return [
        'name' => $faker->words(random_int(3, 7), true),
        'quantity' => (string)$quantityTypeAmount . ' ' . $quantityTypeString[0]
    ];
});
$factory->define(Step::class, static function (Faker $faker) {
    static $step_order = 1;
    return [
        'step_order' => $step_order++,
        'description' => $faker->sentences(random_int(1, 4), true),
    ];
});
// Create 5 users.
factory(User::class, 5)->create()->each(static function (User $user) {
    // Create 5 - 10 recipes for each user.
    factory(Recipe::class, 5)->create(['user_id'=>$user->id])->each(static function (Recipe $recipe) {

        // For each recipe, create 5 - 10 ingredients randomly.
        $recipe->ingredients()->saveMany(factory(Ingredient::class, random_int(3,7))->make());

        // For each recipe, create 1 - 5 steps for each recipe
        $recipe->steps()->saveMany(factory(Step::class,random_int(1,5))->make());

    });
});
交互系数:

if (!Schema::hasTable('recipe_ingredients')) {
   Schema::create('recipe_ingredients', static function (Blueprint $table) {
     $table->unsignedBigInteger('recipe_id');
     $table->string('name', 100);
     $table->string('quantity', '15');
     $table->timestamps();
     $table->foreign('recipe_id')->references('id')->on('recipes')->onDelete('cascade');
    });
}
if (!Schema::hasTable('recipe_steps')) {
   Schema::create('recipe_steps', static function (Blueprint $table){
      $table->unsignedBigInteger('recipe_id');
      $table->unsignedInteger('step_order');
      $table->text('description');
      $table->timestamps();
      $table->foreign('recipe_id')->references('id')->on('recipes')->onDelete('cascade');
   });
}
public function User(): \Illuminate\Database\Eloquent\Relations\BelongsTo
    {
        /** @var Recipe $this */
        return $this->belongsTo(User::class);
    }

    /**
     * @return HasMany
     */
    public function ingredients(): HasMany
    {
        return $this->hasMany(Ingredient::class);
    }

    public function steps(): HasMany
    {
        return $this->hasMany(Ingredient::class);
    }
public function Recipe(): \Illuminate\Database\Eloquent\Relations\BelongsTo
{
    /** @var Ingredient $this */
    return $this->belongsTo(Recipe::class);
}
public function Recipe(): BelongsTo
{
     /** @var Ingredient $this */
     return $this->belongsTo(Recipe::class);
}
$factory->define(Recipe::class, static function (Faker $faker) {
    return [
        'user_id' => factory(User::class)->create()->id,
        'title' => rtrim($faker->sentence(random_int(5, 10)), '.'),
        'description' => $faker->paragraphs(random_int(3, 7), true),
    ];
});
$factory->define(Ingredient::class, static function (Faker $faker) {
    $quantityTypeAmount = $faker->randomFloat(2, 0, 10);
    $quantityTypeString = $faker->randomElements(
        ['tsp.', 'tbsp.', 'fl oz.', 'gill.', 'cup.', 'pt.', 'qt.', 'gal.', 'ml.', 'L', 'dl',]
    );
    return [
        'name' => $faker->words(random_int(3, 7), true),
        'quantity' => (string)$quantityTypeAmount . ' ' . $quantityTypeString[0]
    ];
});
$factory->define(Step::class, static function (Faker $faker) {
    static $step_order = 1;
    return [
        'step_order' => $step_order++,
        'description' => $faker->sentences(random_int(1, 4), true),
    ];
});
// Create 5 users.
factory(User::class, 5)->create()->each(static function (User $user) {
    // Create 5 - 10 recipes for each user.
    factory(Recipe::class, 5)->create(['user_id'=>$user->id])->each(static function (Recipe $recipe) {

        // For each recipe, create 5 - 10 ingredients randomly.
        $recipe->ingredients()->saveMany(factory(Ingredient::class, random_int(3,7))->make());

        // For each recipe, create 1 - 5 steps for each recipe
        $recipe->steps()->saveMany(factory(Step::class,random_int(1,5))->make());

    });
});
接收请求工厂:

if (!Schema::hasTable('recipe_ingredients')) {
   Schema::create('recipe_ingredients', static function (Blueprint $table) {
     $table->unsignedBigInteger('recipe_id');
     $table->string('name', 100);
     $table->string('quantity', '15');
     $table->timestamps();
     $table->foreign('recipe_id')->references('id')->on('recipes')->onDelete('cascade');
    });
}
if (!Schema::hasTable('recipe_steps')) {
   Schema::create('recipe_steps', static function (Blueprint $table){
      $table->unsignedBigInteger('recipe_id');
      $table->unsignedInteger('step_order');
      $table->text('description');
      $table->timestamps();
      $table->foreign('recipe_id')->references('id')->on('recipes')->onDelete('cascade');
   });
}
public function User(): \Illuminate\Database\Eloquent\Relations\BelongsTo
    {
        /** @var Recipe $this */
        return $this->belongsTo(User::class);
    }

    /**
     * @return HasMany
     */
    public function ingredients(): HasMany
    {
        return $this->hasMany(Ingredient::class);
    }

    public function steps(): HasMany
    {
        return $this->hasMany(Ingredient::class);
    }
public function Recipe(): \Illuminate\Database\Eloquent\Relations\BelongsTo
{
    /** @var Ingredient $this */
    return $this->belongsTo(Recipe::class);
}
public function Recipe(): BelongsTo
{
     /** @var Ingredient $this */
     return $this->belongsTo(Recipe::class);
}
$factory->define(Recipe::class, static function (Faker $faker) {
    return [
        'user_id' => factory(User::class)->create()->id,
        'title' => rtrim($faker->sentence(random_int(5, 10)), '.'),
        'description' => $faker->paragraphs(random_int(3, 7), true),
    ];
});
$factory->define(Ingredient::class, static function (Faker $faker) {
    $quantityTypeAmount = $faker->randomFloat(2, 0, 10);
    $quantityTypeString = $faker->randomElements(
        ['tsp.', 'tbsp.', 'fl oz.', 'gill.', 'cup.', 'pt.', 'qt.', 'gal.', 'ml.', 'L', 'dl',]
    );
    return [
        'name' => $faker->words(random_int(3, 7), true),
        'quantity' => (string)$quantityTypeAmount . ' ' . $quantityTypeString[0]
    ];
});
$factory->define(Step::class, static function (Faker $faker) {
    static $step_order = 1;
    return [
        'step_order' => $step_order++,
        'description' => $faker->sentences(random_int(1, 4), true),
    ];
});
// Create 5 users.
factory(User::class, 5)->create()->each(static function (User $user) {
    // Create 5 - 10 recipes for each user.
    factory(Recipe::class, 5)->create(['user_id'=>$user->id])->each(static function (Recipe $recipe) {

        // For each recipe, create 5 - 10 ingredients randomly.
        $recipe->ingredients()->saveMany(factory(Ingredient::class, random_int(3,7))->make());

        // For each recipe, create 1 - 5 steps for each recipe
        $recipe->steps()->saveMany(factory(Step::class,random_int(1,5))->make());

    });
});
配方步骤工厂:

if (!Schema::hasTable('recipe_ingredients')) {
   Schema::create('recipe_ingredients', static function (Blueprint $table) {
     $table->unsignedBigInteger('recipe_id');
     $table->string('name', 100);
     $table->string('quantity', '15');
     $table->timestamps();
     $table->foreign('recipe_id')->references('id')->on('recipes')->onDelete('cascade');
    });
}
if (!Schema::hasTable('recipe_steps')) {
   Schema::create('recipe_steps', static function (Blueprint $table){
      $table->unsignedBigInteger('recipe_id');
      $table->unsignedInteger('step_order');
      $table->text('description');
      $table->timestamps();
      $table->foreign('recipe_id')->references('id')->on('recipes')->onDelete('cascade');
   });
}
public function User(): \Illuminate\Database\Eloquent\Relations\BelongsTo
    {
        /** @var Recipe $this */
        return $this->belongsTo(User::class);
    }

    /**
     * @return HasMany
     */
    public function ingredients(): HasMany
    {
        return $this->hasMany(Ingredient::class);
    }

    public function steps(): HasMany
    {
        return $this->hasMany(Ingredient::class);
    }
public function Recipe(): \Illuminate\Database\Eloquent\Relations\BelongsTo
{
    /** @var Ingredient $this */
    return $this->belongsTo(Recipe::class);
}
public function Recipe(): BelongsTo
{
     /** @var Ingredient $this */
     return $this->belongsTo(Recipe::class);
}
$factory->define(Recipe::class, static function (Faker $faker) {
    return [
        'user_id' => factory(User::class)->create()->id,
        'title' => rtrim($faker->sentence(random_int(5, 10)), '.'),
        'description' => $faker->paragraphs(random_int(3, 7), true),
    ];
});
$factory->define(Ingredient::class, static function (Faker $faker) {
    $quantityTypeAmount = $faker->randomFloat(2, 0, 10);
    $quantityTypeString = $faker->randomElements(
        ['tsp.', 'tbsp.', 'fl oz.', 'gill.', 'cup.', 'pt.', 'qt.', 'gal.', 'ml.', 'L', 'dl',]
    );
    return [
        'name' => $faker->words(random_int(3, 7), true),
        'quantity' => (string)$quantityTypeAmount . ' ' . $quantityTypeString[0]
    ];
});
$factory->define(Step::class, static function (Faker $faker) {
    static $step_order = 1;
    return [
        'step_order' => $step_order++,
        'description' => $faker->sentences(random_int(1, 4), true),
    ];
});
// Create 5 users.
factory(User::class, 5)->create()->each(static function (User $user) {
    // Create 5 - 10 recipes for each user.
    factory(Recipe::class, 5)->create(['user_id'=>$user->id])->each(static function (Recipe $recipe) {

        // For each recipe, create 5 - 10 ingredients randomly.
        $recipe->ingredients()->saveMany(factory(Ingredient::class, random_int(3,7))->make());

        // For each recipe, create 1 - 5 steps for each recipe
        $recipe->steps()->saveMany(factory(Step::class,random_int(1,5))->make());

    });
});
数据库播种机:

if (!Schema::hasTable('recipe_ingredients')) {
   Schema::create('recipe_ingredients', static function (Blueprint $table) {
     $table->unsignedBigInteger('recipe_id');
     $table->string('name', 100);
     $table->string('quantity', '15');
     $table->timestamps();
     $table->foreign('recipe_id')->references('id')->on('recipes')->onDelete('cascade');
    });
}
if (!Schema::hasTable('recipe_steps')) {
   Schema::create('recipe_steps', static function (Blueprint $table){
      $table->unsignedBigInteger('recipe_id');
      $table->unsignedInteger('step_order');
      $table->text('description');
      $table->timestamps();
      $table->foreign('recipe_id')->references('id')->on('recipes')->onDelete('cascade');
   });
}
public function User(): \Illuminate\Database\Eloquent\Relations\BelongsTo
    {
        /** @var Recipe $this */
        return $this->belongsTo(User::class);
    }

    /**
     * @return HasMany
     */
    public function ingredients(): HasMany
    {
        return $this->hasMany(Ingredient::class);
    }

    public function steps(): HasMany
    {
        return $this->hasMany(Ingredient::class);
    }
public function Recipe(): \Illuminate\Database\Eloquent\Relations\BelongsTo
{
    /** @var Ingredient $this */
    return $this->belongsTo(Recipe::class);
}
public function Recipe(): BelongsTo
{
     /** @var Ingredient $this */
     return $this->belongsTo(Recipe::class);
}
$factory->define(Recipe::class, static function (Faker $faker) {
    return [
        'user_id' => factory(User::class)->create()->id,
        'title' => rtrim($faker->sentence(random_int(5, 10)), '.'),
        'description' => $faker->paragraphs(random_int(3, 7), true),
    ];
});
$factory->define(Ingredient::class, static function (Faker $faker) {
    $quantityTypeAmount = $faker->randomFloat(2, 0, 10);
    $quantityTypeString = $faker->randomElements(
        ['tsp.', 'tbsp.', 'fl oz.', 'gill.', 'cup.', 'pt.', 'qt.', 'gal.', 'ml.', 'L', 'dl',]
    );
    return [
        'name' => $faker->words(random_int(3, 7), true),
        'quantity' => (string)$quantityTypeAmount . ' ' . $quantityTypeString[0]
    ];
});
$factory->define(Step::class, static function (Faker $faker) {
    static $step_order = 1;
    return [
        'step_order' => $step_order++,
        'description' => $faker->sentences(random_int(1, 4), true),
    ];
});
// Create 5 users.
factory(User::class, 5)->create()->each(static function (User $user) {
    // Create 5 - 10 recipes for each user.
    factory(Recipe::class, 5)->create(['user_id'=>$user->id])->each(static function (Recipe $recipe) {

        // For each recipe, create 5 - 10 ingredients randomly.
        $recipe->ingredients()->saveMany(factory(Ingredient::class, random_int(3,7))->make());

        // For each recipe, create 1 - 5 steps for each recipe
        $recipe->steps()->saveMany(factory(Step::class,random_int(1,5))->make());

    });
});
我没有收到任何错误消息

对于每个配方,我希望配方步骤表中的步骤顺序从1增加到n。但是,目前每个配方的步骤顺序继续递增

例如,目前,我得到:

recipe_id - step_order - description 1 1 ------- 1 2 ------- /\/\/\/\/\/\/\/\/\/\/\/\/\/ 1 10 ------- 2 11 ------- 2 12 ------- etc... 配方标识-步骤顺序-说明 1 1 ------- 1 2 ------- /\/\/\/\/\/\/\/\/\/\/\/\/\/ 1 10 ------- 2 11 ------- 2 12 ------- 等 我期待的是:

recipe_id - step_order - description 1 1 ------- 1 2 ------- /\/\/\/\/\/\/\/\/\/\/\/\/\/ 1 10 ------- 2 1 ------- 2 2 ------- 2 3 ------- 3 1 ------- 3 2 ------- etc... 配方标识-步骤顺序-说明 1 1 ------- 1 2 ------- /\/\/\/\/\/\/\/\/\/\/\/\/\/ 1 10 ------- 2 1 ------- 2 2 ------- 2 3 ------- 3 1 ------- 3 2 ------- 等 我想我需要某种中间桌子。我读过的与此相关的另一个术语是透视表。我唯一熟悉的是中间表,但我不知道如何在Laravel中实现中间表,如果我需要的话


如果您需要进一步的澄清,请告诉我。

我将从RecipeStepsFactory中删除第二行,并将“步骤顺序”的默认值设置为1

$factory->define(Step::class, static function (Faker $faker) {
    return [
        'step_order' => 1, // default value of 1
        'description' => $faker->sentences(random_int(1, 4), true),
    ];
});
然后在DatabaseSeeder文件中,我将执行配方播种器,如下所示

$steps = random_int(1,5);

// For each recipe, create 1 - 5 steps for each recipe
for ($i = 1; $i <= $steps; $i++) {
   $recipe->steps()->save(factory(Step::class)->make(['step_order' => $i]));
}

$steps=random_int(1,5);
//对于每个配方,为每个配方创建1-5个步骤
对于($i=1;$i步骤()->save(工厂(步骤::类)->make(['Step\u order'=>$i]);
}
很有效。:)谢谢!我必须将for循环初始值设定项设置为1,否则它将从0开始。