-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFixRecentlyCreated.php
36 lines (32 loc) · 1015 Bytes
/
FixRecentlyCreated.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
<?php declare(strict_types = 1);
namespace LastDragon_ru\LaraASP\Testing\Database\Eloquent\Factories;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
use Override;
/**
* After creating the model will have `wasRecentlyCreated = true`, in most
* cases this is unwanted behavior, this trait fixes it.
*
* @phpstan-require-extends Factory
*/
trait FixRecentlyCreated {
/**
* @inheritDoc
*
* @param Collection<array-key,Model> $instances
*/
#[Override]
protected function callAfterCreating(Collection $instances, ?Model $parent = null) {
$this->fixRecentlyCreated($instances);
parent::callAfterCreating($instances, $parent);
}
/**
* @param Collection<array-key,Model> $instances
*/
private function fixRecentlyCreated(Collection $instances): void {
foreach ($instances as $instance) {
$instance->wasRecentlyCreated = false;
}
}
}