[英]Laravel factory for a relationship with multiple columns
为我正在处理的项目创建工厂,我在创建 belongsTo 关系时遇到了问题。
在我的Cards
表中,我有 2 列引用相同的关系: Card->order->id
和Card->order->ref
(不要问我为什么,我什至不知道)。
因此,在我的CardFactory
中,我需要能够为$card->order_ref
和$card->order_id
使用相同的Order
实例。 使用工厂状态,我确实创建了以下 state:
public function sold(int $userId) : Factory
{
$createdOrder = Order::factory()->forUser($userId)->create();
return $this->state(function (array $attributes) use ($createdOrder) {
return [
'state' => Card::STATE_SOLD,
'sold' => Carbon::now()->subDay(),
'addedincart' => Carbon::now()->subDay()->subHour(),
'selling_date' => Carbon::now()->subDay(),
'order_id' => $createdOrder->id,
'order_ref' => $createdOrder->ref,
];
});
}
我的OrderFactory.php
:
class OrderFactory extends Factory {
public function definition() : array
{
return [
'ref' => $this->faker->randomNumber(9) ,
'state' => 5,
'clientname' => $this->faker->name(),
'email' => $this->faker->email(),
'phone' => $this->faker->phoneNumber(),
'address' => null,
'shippingaddress' => NULL,
'total' => $this->faker->randomFloat(2, 50, 700),
'method' => 'creditcard',
'user_id' => null,
'tracking' => NULL,
'response' => json_encode(["someFields" => "someValues"]),
'paid_on' => now(),
'sent_on' => now(),
'review_sent' => '1',
'deliverymethod' => 'email',
'deliverycosts' => '0.00',
'coupon_id' => NULL,
'discount' => '0.00',
'explanation' => NULL,
'refund_amount' => NULL,
'refunded_at' => NULL,
'shipping_status' => NULL,
'original_cards' => '[1, 2]',
'cancel_reason' => NULL,
'private_comment' => NULL,
'loyalty' => '30',
'ip' => '00.00.00.00',
'cb' => 'someCb',
'geoip' => NULL,
'ecards_sent' => '1',
'blocked_by' => NULL,
'partner' => NULL,
'original_giftcards' => '',
'giftcards_sent' => '0',
'api_data' => NULL,
'original_products' => NULL,
'delivered_at' => now(),
'invoice_send_at' => NULL,
'type' => 'cards',
'redeem_id' => '0',
];
}
public function forUser(int $id) : Factory
{
return $this->state(function (array $attributes) use ($id) {
return [
'user_id' => $id,
];
});
}
}
在我的CardSeeder.php
中:
public function run()
{
$user = User::query()->where("email", "myAddress@gmail.com")->first();
Card::factory()->sold($user->id)->count(15)->create();
Card::factory()->selling()->count(15)->create();
}
问题是,对于每个sold
的 state 创建,都使用相同的$createdOrder->id
。
对于每一行,我怎么能使用不同的顺序?
作为文档https://laravel.com/docs/9.x/database-testing#defining-relationships-within-factories
描述:
如果关系的列依赖于定义它的工厂,您可以将闭包分配给属性。 闭包将接收工厂的评估属性数组。
所以如果你想生成关系的新实例,你必须在闭包内创建它:
public function sold(int $userId) : Factory
{
return $this->state(function (array $attributes) use ($userId) {
$order=null;
return [
'state' => Card::STATE_SOLD,
'sold' => Carbon::now()->subDay(),
'addedincart' => Carbon::now()->subDay()->subHour(),
'selling_date' => Carbon::now()->subDay(),
'order_id' => function()use(&$order,$userId){
$order = Order::factory()->forUser($userId)->create();
return $order->id;
},
'order_ref' => function()use(&$order){
return $order->ref;
},
];
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.