5 months ago

Database Seeding

Posted 5 months ago by nanadjei2

My app allows users to place an order. I render two types of services which are Wash and fold and Wash and iron. and each of them has packages under them. Wash and fold take only one package at a time but wash and iron can take multiple packages. Anytime an order is placed, I send the order_id and the service_package_id to a separate table so that I can track the type of service package for a particular order. I am seeding my database and want packages under wash and iron to have multiple service packages. Eg:

public function run(Faker $faker)
      $orders = \App\Order::all();

      foreach ($orders as $order) {
        if ($order->service->slug == 'wash-and-fold') {
            factory(\App\OrderEstimatedWeights::class, 1)->create([
                'order_id' => $order->id,
                // 'service_package_id' => ServicePackage::where('service_id', $order->service->id)->get()->random()->id,
                'service_package_id' => $faker->numberBetween(1, 4)
         } else {
            factory(\App\OrderEstimatedWeights::class, 4)->create([ // Create 4
                'order_id' => $order->id,
                // 'service_package_id' => ServicePackage::where('service_id', $order->service->id)->get()->random()->id,
                'service_package_id' => $faker->numberBetween(4, 8), // Each should have different number between 4 and 8
                'number_of_items' => $faker->numberBetween(5, 15)

The 1st condition statement works fine but the next one (else), 'service_package_id' => $faker->numberBetween(4, 8) and 'number_of_items' => $faker->numberBetween(5, 15)

Screenshot of my db here...

keeps repeating the same numbers on each create instance. I hope to get help. Thank you.

Please sign in or create an account to participate in this conversation.