如何在Laravel的额外字段中为pivot数据库表添加伪造值种子?

如何在Laravel的额外字段中为pivot数据库表添加伪造值种子?,laravel,pivot,factory,seed,faker,Laravel,Pivot,Factory,Seed,Faker,我想在我的数据库中植入伪造的值。我有4个数据库表:User、Order、Products和Order\u Products。最后一个表是订单和产品之间的数据透视表。我在这个透视表中添加了两个字段:金额和价格。现在我想用伪造的数据填充所有这些表。如果我留下两个额外的字段,它将与下面的代码一起工作。如果我在迁移中添加两个额外字段。我得到一个错误(很明显)。但是如何从透视表中填充额外字段呢?提前谢谢 它在我的透视表中没有额外字段的情况下工作,但我知道我希望额外字段也填充伪造数据 工厂订单 工厂产品 播

我想在我的数据库中植入伪造的值。我有4个数据库表:User、Order、Products和Order\u Products。最后一个表是订单和产品之间的数据透视表。我在这个透视表中添加了两个字段:金额和价格。现在我想用伪造的数据填充所有这些表。如果我留下两个额外的字段,它将与下面的代码一起工作。如果我在迁移中添加两个额外字段。我得到一个错误(很明显)。但是如何从透视表中填充额外字段呢?提前谢谢

它在我的透视表中没有额外字段的情况下工作,但我知道我希望额外字段也填充伪造数据

工厂订单 工厂产品 播种机 迁移数据透视表 试试这个

 public function run ()
 {
  factory ( App\User::class, 5 )->create ()
     ->each ( function ( $user ) {
        $user->orders ()->saveMany ( factory ( App\Order::class, 2 )->create ( [ 'user_id' => $user->id ] )
              ->each ( function ( $order ) {
                 $products = factory ( App\Product::class, 3 )->make ();
                 foreach ( $products as $product )
                 {
                    $order->products ()->attach ( $product->id, [ 'amount' => 'xxx', 'price' => 'xxx' ] );
                 }
              } )
           );
     } );
}试试这个

 public function run ()
 {
  factory ( App\User::class, 5 )->create ()
     ->each ( function ( $user ) {
        $user->orders ()->saveMany ( factory ( App\Order::class, 2 )->create ( [ 'user_id' => $user->id ] )
              ->each ( function ( $order ) {
                 $products = factory ( App\Product::class, 3 )->make ();
                 foreach ( $products as $product )
                 {
                    $order->products ()->attach ( $product->id, [ 'amount' => 'xxx', 'price' => 'xxx' ] );
                 }
              } )
           );
     } );
}

public function run()
{
   factory(App\User::class, 5)->create()->each(function ($user) {
            $user->orders()
                 ->saveMany(factory(App\Order::class, 2)
                 ->create(['user_id' => $user->id])
                 ->each(function ($order){
                      $order->products()
                      ->saveMany(factory(App\Product::class, 3)
                      ->create());
                      })
                      );
            });
}
public function up()
{
    Schema::create('order_product', function (Blueprint $table) {
        $table->unsignedBigInteger('order_id');      
        $table->unsignedBigInteger('product_id');
        $table->integer('amount');
        $table->float('price',8,2);
    });

    Schema::table('order_product', function($table) {
        $table->foreign('product_id')->references('id')->on('products')->onDelete('cascade');
        $table->foreign('order_id')->references('id')->on('orders')->onDelete('cascade');
        $table->primary(['order_id', 'product_id']);
    });
}
 public function run ()
 {
  factory ( App\User::class, 5 )->create ()
     ->each ( function ( $user ) {
        $user->orders ()->saveMany ( factory ( App\Order::class, 2 )->create ( [ 'user_id' => $user->id ] )
              ->each ( function ( $order ) {
                 $products = factory ( App\Product::class, 3 )->make ();
                 foreach ( $products as $product )
                 {
                    $order->products ()->attach ( $product->id, [ 'amount' => 'xxx', 'price' => 'xxx' ] );
                 }
              } )
           );
     } );