Seeding Order Item data for miniecommerce

// artisan command for migration, factory and seeder file 
php artisan make:model OrderItem -mfs

// migration file 
public function up()
{
	Schema::create('order_items', function (Blueprint $table) {
		$table->id();
		$table->foreignId('order_id')->constrained();
		$table->foreignId('product_id')->constrained();
		$table->foreignId('profile_id')->constrained();
		$table->foreignId('frame_width_id')->constrained();
		$table->integer('width');
		$table->integer('height');
		$table->float('total_square_feet');
		$table->integer('quantity');
		$table->float('cost');
		$table->text('additional_request');               
		$table->timestamps();
	});
}

// OrderItemFactory.php file 
class OrderItemFactory extends Factory
{
    /**
     * Define the model's default state.
     *
     * @return array
     */
    public function definition()
    {
        $orderIds = Order::pluck('id');
        $prodIds = Product::pluck('id');
        $profileIds = Profile::pluck('id');
        $fwIds = FrameWidth::pluck('id');
        return [
            'order_id' => $orderIds->random(),
            'product_id' => $prodIds->random(),
            'profile_id' => $profileIds->random(),
            'frame_width_id' => $fwIds->random(),
            'width' => $this->faker->randomDigit(),
            'height' => $this->faker->randomDigit(),
            'total_square_feet' => $this->faker->randomFloat(2, 1, 2),
            'quantity' => $this->faker->randomDigit(),
            'cost' => $this->faker->randomFloat(2, 10, 40),
            'additional_request' =>  $this->faker->text(100)
        ];
    }
}

// OrderItemSeeder.php file 
class OrderItemSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        //
        OrderItem::factory(30)->create();
    }
}

Related Posts


Building mini ecommerce in Laravel

Listing rows of users - crud

Adding user data - crud of users

Editing user data - crud of users

Deleting user data - crud of users

Listing rows of products - crud

Listing rows of profiles - crud

Listing rows of orders - crud

Listing rows of order items - crud