我有以下模型工厂:
$factory->define(App\User::class, function (Faker $faker) {
return [
'first_name' => $faker->firstName,
'last_name' => $faker->lastName,
'email' => $faker->unique()->safeEmail,
'password' => 'secret',
'remember_token' => Str::random(10),
];
});
$factory->define(App\Order::class, function (Faker $faker) {
return [
'tax_id' => 3,
'total' => 0.00,
'order_type' => 'Invoice'
];
});
$factory->define(App\OrderItem::class, function (Faker $faker) {
return [
'product_id' => 19,
'product_name' => 'Free product',
'quantity' => 1,
'item_price' => 0.00,
];
});
$factory->afterCreating(App\User::class, function ($user, $faker) {
$user->orders()->save(factory(App\Order::class)->make());
});
$factory->afterMaking(App\Order::class, function ($order, $faker) {
$order->orderItems()->save(factory(App\OrderItem::class)->make());
});
当我打电话时,factory(App\User::class, 2)->create();
我得到错误:
Doctrine\DBAL\Driver\PDOException::("SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'order_id' cannot be null")
为什么这些回调方法不起作用,任何建议表示赞赏?