回答編集履歴

2

更に良いやり方が提供されたのでそれを反映しました。

2019/01/23 07:37

投稿

nobuko
nobuko

スコア12

test CHANGED
@@ -49,3 +49,27 @@
49
49
  });
50
50
 
51
51
  ```
52
+
53
+
54
+
55
+ さらにafter(mix-peachさんより)
56
+
57
+ ```ここに言語を入力
58
+
59
+ factory(App\Customer::class, 10)->create()->each(function ($customer) {
60
+
61
+
62
+
63
+ $customer->billing_address_id = factory(App\CustomerAddress::class)->create(['customer_id' => $customer->id])->id;
64
+
65
+ $customer->shipping_address_id = factory(App\CustomerAddress::class)->create(['customer_id' => $customer->id])->id;
66
+
67
+
68
+
69
+ $customer->save();
70
+
71
+
72
+
73
+ });
74
+
75
+ ```

1

リファクタリングしました。

2019/01/23 07:37

投稿

nobuko
nobuko

スコア12

test CHANGED
@@ -28,13 +28,9 @@
28
28
 
29
29
  factory(App\CustomerAddress::class, 2)->create(['customer_id' => $customer->id])->each(function ($customer_address) {
30
30
 
31
- global $i;
32
-
33
31
  global $customer_id;
34
32
 
35
- isset($i) ? $i++ : $i = 1;
36
-
37
- if ($i % 2 == 1) {
33
+ if ($customer_address->id % 2 == 1) {
38
34
 
39
35
  $column = 'billing_address_id';
40
36