Skip to content

Commit 98cd248

Browse files
authored
Merge pull request #99 from ycs77/add-trait-for-model
Static call from model
2 parents f3fae40 + 8666652 commit 98cd248

File tree

2 files changed

+123
-0
lines changed

2 files changed

+123
-0
lines changed

README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,32 @@ Array
161161
)
162162
```
163163

164+
# Example called from model
164165

166+
Add `HasBatch` trait into model:
167+
168+
```php
169+
namespace App\Models;
170+
171+
use Mavinoo\Batch\Traits\HasBatch;
172+
173+
class User extends Model
174+
{
175+
use HasBatch;
176+
}
177+
```
178+
179+
And call `batchUpdate()` or `batchInsert()` from model:
180+
181+
```php
182+
use App\Models\User;
183+
184+
// ex: update
185+
User::batchUpdate($value, $index);
186+
187+
// ex: insert
188+
User::batchInsert($columns, $values, $batchSize);
189+
```
165190

166191
# Helper batch()
167192

src/Traits/HasBatch.php

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Mavinoo\Batch\Traits;
4+
5+
use Mavinoo\Batch\Batch;
6+
7+
trait HasBatch
8+
{
9+
/**
10+
* Update multiple rows.
11+
*
12+
* Example:
13+
* ```
14+
* use App\Models\User;
15+
*
16+
* $values = [
17+
* [
18+
* 'id' => 1,
19+
* 'status' => 'active',
20+
* 'nickname' => 'Mohammad',
21+
* ],
22+
* [
23+
* 'id' => 5,
24+
* 'status' => 'deactive',
25+
* 'nickname' => 'Ghanbari',
26+
* ],
27+
* [
28+
* 'id' => 7,
29+
* 'balance' => ['+', 500],
30+
* ],
31+
* ];
32+
*
33+
* User::batchUpdate($values, 'id');
34+
* ```
35+
*
36+
* @param array $values
37+
* @param string|null $index
38+
* @param bool $raw
39+
* @return bool|int
40+
*/
41+
public static function batchUpdate(array $values, string $index = null, bool $raw = false)
42+
{
43+
return app(Batch::class)->update(new static, $values, $index, $raw);
44+
}
45+
46+
/**
47+
* Insert multiple rows.
48+
*
49+
* Example:
50+
* ```
51+
* use App\Models\User;
52+
*
53+
* $columns = [
54+
* 'firstName',
55+
* 'lastName',
56+
* 'email',
57+
* 'isActive',
58+
* 'status',
59+
* ];
60+
* $values = [
61+
* [
62+
* 'Mohammad',
63+
* 'Ghanbari',
64+
65+
* '1',
66+
* '0',
67+
* ] ,
68+
* [
69+
* 'Saeed',
70+
* 'Mohammadi',
71+
72+
* '1',
73+
* '0',
74+
* ] ,
75+
* [
76+
* 'Avin',
77+
* 'Ghanbari',
78+
79+
* '1',
80+
* '0',
81+
* ] ,
82+
* ];
83+
* $batchSize = 500; // insert 500 (default), 100 minimum rows in one query
84+
*
85+
* User::batchInsert($columns, $values, $batchSize);
86+
* ```
87+
*
88+
* @param array $columns
89+
* @param array $values
90+
* @param int $batchSize
91+
* @param bool $insertIgnore
92+
* @return bool|array
93+
*/
94+
public static function batchInsert(array $columns, array $values, int $batchSize = 500, bool $insertIgnore = false)
95+
{
96+
return app(Batch::class)->insert(new static, $columns, $values, $batchSize, $insertIgnore);
97+
}
98+
}

0 commit comments

Comments
 (0)