如何在 Laravel 的额外字段中使用伪造值为数据透视数据库 table 播种?
How to seed a pivot database table with faker values in extra fields in Laravel?
我想用伪造的值来为我的数据库做种。我有 4 个数据库 table:用户、订单、产品和 Order_Products。最后一个 table 是订单和产品之间的支点 table。我向这个数据透视表 table 添加了两个字段:金额和价格。现在我想用 faker 数据填充所有这些 table。如果我留下两个额外的字段,它可以与下面的代码一起使用。如果我在迁移中添加两个额外的字段。我收到一个错误(很明显)。但是我如何填写 Pivot table 中的额外字段?提前致谢。
它在我的数据透视表中没有额外字段的情况下工作 table,但我知道我希望额外字段也填充伪造数据。
工厂订单
$factory->define(App\Order::class, function (Faker $faker) {
return [
'orderdate' => now(),
'amount' => $faker->randomFloat(2,0,6)
];
});
工厂产品
$factory->define(App\Product::class, function (Faker $faker) {
return [
'name' => $faker->name,
'description' => $faker->sentence(),
'price'=> $faker->randomFloat(2,0,6)
];
});
播种机
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());
})
);
});
}
迁移枢轴Table
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' ] );
}
} )
);
} );
}
我想用伪造的值来为我的数据库做种。我有 4 个数据库 table:用户、订单、产品和 Order_Products。最后一个 table 是订单和产品之间的支点 table。我向这个数据透视表 table 添加了两个字段:金额和价格。现在我想用 faker 数据填充所有这些 table。如果我留下两个额外的字段,它可以与下面的代码一起使用。如果我在迁移中添加两个额外的字段。我收到一个错误(很明显)。但是我如何填写 Pivot table 中的额外字段?提前致谢。
它在我的数据透视表中没有额外字段的情况下工作 table,但我知道我希望额外字段也填充伪造数据。
工厂订单
$factory->define(App\Order::class, function (Faker $faker) {
return [
'orderdate' => now(),
'amount' => $faker->randomFloat(2,0,6)
];
});
工厂产品
$factory->define(App\Product::class, function (Faker $faker) {
return [
'name' => $faker->name,
'description' => $faker->sentence(),
'price'=> $faker->randomFloat(2,0,6)
];
});
播种机
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());
})
);
});
}
迁移枢轴Table
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' ] );
}
} )
);
} );
}