-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWithoutModelEvents.php
46 lines (41 loc) · 1.12 KB
/
WithoutModelEvents.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<?php declare(strict_types = 1);
namespace LastDragon_ru\LaraASP\Testing\Database\Eloquent\Factories;
use Closure;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Database\Eloquent\Model;
use Override;
/**
* Disable models events during make/create.
*
* @phpstan-require-extends Factory
*/
trait WithoutModelEvents {
/**
* @inheritDoc
*/
#[Override]
public function make($attributes = [], ?Model $parent = null) {
return $this->callWithoutModelEvents(function () use ($attributes, $parent) {
return parent::make($attributes, $parent);
});
}
/**
* @inheritDoc
*/
#[Override]
public function create($attributes = [], ?Model $parent = null) {
return $this->callWithoutModelEvents(function () use ($attributes, $parent) {
return parent::create($attributes, $parent);
});
}
/**
* @template T
*
* @param Closure(): T $closure
*
* @return T
*/
private function callWithoutModelEvents(Closure $closure): mixed {
return $this->modelName()::withoutEvents($closure);
}
}