1 | <?php |
||||
2 | |||||
3 | namespace CoordinativeDev\Fixture; |
||||
4 | |||||
5 | use Exception; |
||||
6 | use Illuminate\Database\Eloquent\SoftDeletes; |
||||
7 | use Illuminate\Support\Facades\Log; |
||||
8 | |||||
9 | /** |
||||
10 | * EloquentFixture represents a fixture backed up by a [[modelClass|Illuminate\Database\Eloquent\Model class]]. |
||||
11 | * |
||||
12 | * [[modelClass]] must be set. You should also provide fixture data in the file |
||||
13 | * specified by [[dataFile]] or overriding [[getData()]] if you want to use code to generate the fixture data. |
||||
14 | * |
||||
15 | * When the fixture is being loaded, it will first call [[deleteModels()]] to remove any existing data in the table. |
||||
16 | * It will then populate the table with the data returned by [[getData()]]. |
||||
17 | * |
||||
18 | * After the fixture is loaded, you can access the loaded data via the [[data]] property. |
||||
19 | */ |
||||
20 | class EloquentFixture extends BaseActiveFixture |
||||
21 | { |
||||
22 | /** |
||||
23 | * @var string the file path that contains the fixture data to be returned by [[getData()]] |
||||
24 | */ |
||||
25 | public $dataFile; |
||||
26 | |||||
27 | /** |
||||
28 | * @inheritdoc |
||||
29 | */ |
||||
30 | public function load(): void |
||||
31 | { |
||||
32 | $this->data = []; |
||||
33 | |||||
34 | foreach ($this->getData() as $alias => $row) { |
||||
35 | $model = app($this->modelClass); |
||||
36 | $model->fill($row)->saveOrFail(); |
||||
0 ignored issues
–
show
|
|||||
37 | |||||
38 | $this->data[$alias] = array_merge($row, ['id' => $model->getKey()]); |
||||
0 ignored issues
–
show
The method
getKey() does not exist on Illuminate\Foundation\Application .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. ![]() |
|||||
39 | } |
||||
40 | } |
||||
41 | |||||
42 | /** |
||||
43 | * @inheritdoc |
||||
44 | */ |
||||
45 | public function unload(): void |
||||
46 | { |
||||
47 | $this->deleteModels(); |
||||
48 | |||||
49 | parent::unload(); |
||||
50 | } |
||||
51 | |||||
52 | /** |
||||
53 | * Removes all existing models. |
||||
54 | * |
||||
55 | * @throws Exception |
||||
56 | */ |
||||
57 | protected function deleteModels(): void |
||||
58 | { |
||||
59 | $model = app($this->modelClass); |
||||
60 | // determine if the model uses `SoftDeletes` trait |
||||
61 | $usesSoftDeletes = in_array(SoftDeletes::class, class_uses($model)); |
||||
62 | |||||
63 | foreach ($model->all() as $value) { |
||||
0 ignored issues
–
show
The method
all() does not exist on Illuminate\Foundation\Application . Did you maybe mean call() ?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. ![]() |
|||||
64 | try { |
||||
65 | if ($usesSoftDeletes) { |
||||
66 | $value->forceDelete(); |
||||
67 | } else { |
||||
68 | $value->delete(); |
||||
69 | } |
||||
70 | } catch (Exception $exception) { |
||||
71 | Log::warning(sprintf('Error during deleting models. Table: %s. Error: %s', $model->getTable(), $exception->getMessage())); |
||||
0 ignored issues
–
show
The method
getTable() does not exist on Illuminate\Foundation\Application .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. ![]() |
|||||
72 | } |
||||
73 | } |
||||
74 | } |
||||
75 | } |
||||
76 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.