@@ -13,13 +13,13 @@ Insert and update batch (bulk) in laravel
13
13
14
14
# Service Provider
15
15
16
- file app.php in array providers :
16
+ File ` app.php ` in array providers:
17
17
18
18
` Mavinoo\Batch\BatchServiceProvider::class, `
19
19
20
20
# Aliases
21
21
22
- file app.php in array aliases :
22
+ File ` app.php ` in array aliases:
23
23
24
24
` 'Batch' => Mavinoo\Batch\BatchFacade::class, `
25
25
@@ -30,31 +30,31 @@ use App\Models\User;
30
30
31
31
$userInstance = new User;
32
32
$arrays = [
33
- [
34
- 'conditions' => ['id' => 1, 'status' => 'active'],
35
- 'columns' => [
36
- 'status' => 'invalid'
37
- 'nickname' => 'mohammad'
38
- ],
39
- ],
40
- [
41
- 'conditions' => ['id' => 2],
42
- 'columns' => [
43
- 'nickname' => 'mavinoo',
44
- 'name' => 'mohammad',
45
- ],
46
- ],
47
- [
48
- 'conditions' => ['id' => 3],
49
- 'columns' => [
50
- 'nickname' => 'ali'
51
- ],
52
- ],
33
+ [
34
+ 'conditions' => ['id' => 1, 'status' => 'active'],
35
+ 'columns' => [
36
+ 'status' => 'invalid',
37
+ 'nickname' => 'mohammad',
38
+ ],
39
+ ],
40
+ [
41
+ 'conditions' => ['id' => 2],
42
+ 'columns' => [
43
+ 'nickname' => 'mavinoo',
44
+ 'name' => 'mohammad',
45
+ ],
46
+ ],
47
+ [
48
+ 'conditions' => ['id' => 3],
49
+ 'columns' => [
50
+ 'nickname' => 'ali',
51
+ ],
52
+ ],
53
53
];
54
54
$keyName = 'id';
55
55
56
56
Batch::updateMultipleCondition($userInstance, $arrays, $keyName);
57
- or
57
+ // or
58
58
batch()->updateMultipleCondition($userInstance, $arrays, $keyName);
59
59
```
60
60
@@ -65,21 +65,21 @@ use App\Models\User;
65
65
66
66
$userInstance = new User;
67
67
$value = [
68
- [
69
- 'id' => 1,
70
- 'status' => 'active',
71
- 'nickname' => 'Mohammad'
72
- ] ,
73
- [
74
- 'id' => 5,
75
- 'status' => 'deactive',
76
- 'nickname' => 'Ghanbari'
77
- ] ,
68
+ [
69
+ 'id' => 1,
70
+ 'status' => 'active',
71
+ 'nickname' => 'Mohammad',
72
+ ] ,
73
+ [
74
+ 'id' => 5,
75
+ 'status' => 'deactive',
76
+ 'nickname' => 'Ghanbari',
77
+ ] ,
78
78
];
79
79
$index = 'id';
80
80
81
81
Batch::update($userInstance, $value, $index);
82
- or
82
+ // or
83
83
batch()->update($userInstance, $values, $index);
84
84
```
85
85
@@ -90,29 +90,29 @@ use App\Models\User;
90
90
91
91
$userInstance = new User;
92
92
$value = [
93
- [
94
- 'id' => 1,
95
- 'status' => 'active'
96
- ],
97
- [
98
- 'id' => 5,
99
- 'status' => 'deactive',
100
- 'nickname' => 'Ghanbari'
101
- ],
102
- [
103
- 'id' => 10,
104
- 'status' => 'active',
105
- 'date' => Carbon::now()
106
- ],
107
- [
108
- 'id' => 11,
109
- 'username' => 'mavinoo'
110
- ]
93
+ [
94
+ 'id' => 1,
95
+ 'status' => 'active',
96
+ ],
97
+ [
98
+ 'id' => 5,
99
+ 'status' => 'deactive',
100
+ 'nickname' => 'Ghanbari',
101
+ ],
102
+ [
103
+ 'id' => 10,
104
+ 'status' => 'active',
105
+ 'date' => Carbon::now(),
106
+ ],
107
+ [
108
+ 'id' => 11,
109
+ 'username' => 'mavinoo',
110
+ ],
111
111
];
112
112
$index = 'id';
113
113
114
114
Batch::update($userInstance, $value, $index);
115
- or
115
+ // or
116
116
batch()->update($userInstance, $values, $index);
117
117
```
118
118
@@ -123,31 +123,31 @@ use App\Models\User;
123
123
124
124
$userInstance = new User;
125
125
$value = [
126
- [
127
- 'id' => 1,
128
- 'balance' => ['+', 500] // Add
129
- ] ,
130
- [
131
- 'id' => 2,
132
- 'balance' => ['-', 200] // Subtract
133
- ] ,
134
- [
135
- 'id' => 3,
136
- 'balance' => ['*', 5] // Multiply
137
- ] ,
138
- [
139
- 'id' => 4,
140
- 'balance' => ['/', 2] // Divide
141
- ] ,
142
- [
143
- 'id' => 5,
144
- 'balance' => ['%', 2] // Modulo
145
- ] ,
126
+ [
127
+ 'id' => 1,
128
+ 'balance' => ['+', 500], // Add
129
+ ] ,
130
+ [
131
+ 'id' => 2,
132
+ 'balance' => ['-', 200], // Subtract
133
+ ] ,
134
+ [
135
+ 'id' => 3,
136
+ 'balance' => ['*', 5], // Multiply
137
+ ] ,
138
+ [
139
+ 'id' => 4,
140
+ 'balance' => ['/', 2], // Divide
141
+ ] ,
142
+ [
143
+ 'id' => 5,
144
+ 'balance' => ['%', 2], // Modulo
145
+ ] ,
146
146
];
147
147
$index = 'id';
148
148
149
149
Batch::update($userInstance, $value, $index);
150
- or
150
+ // or
151
151
batch()->update($userInstance, $values, $index);
152
152
```
153
153
@@ -158,44 +158,44 @@ use App\Models\User;
158
158
159
159
$userInstance = new User;
160
160
$columns = [
161
- 'firstName',
162
- 'lastName',
163
- 'email',
164
- 'isActive',
165
- 'status',
161
+ 'firstName',
162
+ 'lastName',
163
+ 'email',
164
+ 'isActive',
165
+ 'status',
166
166
];
167
167
$values = [
168
- [
169
- 'Mohammad',
170
- 'Ghanbari',
171
-
172
- '1',
173
- '0',
174
- ] ,
175
- [
176
- 'Saeed',
177
- 'Mohammadi',
178
-
179
- '1',
180
- '0',
181
- ] ,
182
- [
183
- 'Avin',
184
- 'Ghanbari',
185
-
186
- '1',
187
- '0',
188
- ] ,
168
+ [
169
+ 'Mohammad',
170
+ 'Ghanbari',
171
+
172
+ '1',
173
+ '0',
174
+ ] ,
175
+ [
176
+ 'Saeed',
177
+ 'Mohammadi',
178
+
179
+ '1',
180
+ '0',
181
+ ] ,
182
+ [
183
+ 'Avin',
184
+ 'Ghanbari',
185
+
186
+ '1',
187
+ '0',
188
+ ] ,
189
189
];
190
190
$batchSize = 500; // insert 500 (default), 100 minimum rows in one query
191
191
192
192
$result = Batch::insert($userInstance, $columns, $values, $batchSize);
193
- or
193
+ // or
194
194
$result = batch()->insert($userInstance, $values, $index);
195
195
```
196
196
197
197
``` php
198
- // result : false or array
198
+ // result: false or array
199
199
200
200
sample array result:
201
201
Array
@@ -237,12 +237,10 @@ User::batchInsert($columns, $values, $batchSize);
237
237
238
238
``` php
239
239
// ex: update
240
-
241
240
$result = batch()->update($userInstance, $value, $index);
242
241
243
242
244
243
// ex: insert
245
-
246
244
$result = batch()->insert($userInstance, $columns, $values, $batchSize);
247
245
```
248
246
0 commit comments