create(); // Seeder for recipe_recipe table. $recipe = Recipe::query()->get(); Recipe::each(function (Recipe $model) use ($recipe) { $offset = rand(0, 9); $length = rand(0, 3); $model->recipes()->saveMany($recipe->slice($offset, $length)); }); // Seeder for recipe_products table. $products = Product::query()->get(); Recipe::each(function (Recipe $model) use ($products) { $offset = rand(0, 24); $length = rand(1, 3); $model->products()->saveMany($products->slice($offset, $length)); }); } }