Skip to content

Commit 7291204

Browse files
Exclude softDeletes columns from factory definition (#565)
1 parent cb8db08 commit 7291204

File tree

3 files changed

+41
-3
lines changed

3 files changed

+41
-3
lines changed

src/Generators/FactoryGenerator.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ protected function buildDefinition(Model $model)
7575
* @var \Blueprint\Models\Column $column
7676
*/
7777
foreach ($fillable as $column) {
78-
if ($column->name() === 'id') {
78+
if (in_array($column->name(), ['id', 'softdeletes', 'softdeletestz'])) {
7979
continue;
8080
}
8181

@@ -185,6 +185,10 @@ protected function buildDefinition(Model $model)
185185
}
186186
}
187187

188+
if (empty($definition)) {
189+
return '//';
190+
}
191+
188192
return trim($definition);
189193
}
190194

@@ -194,10 +198,15 @@ private function fillableColumns(array $columns): array
194198
return $columns;
195199
}
196200

197-
return array_filter(
201+
$nonNullableColumns = array_filter(
198202
$columns,
199203
fn (Column $column) => !in_array('nullable', $column->modifiers())
200204
);
205+
206+
return array_filter(
207+
$nonNullableColumns,
208+
fn (Column $column) => $column->dataType() !== 'softDeletes'
209+
);
201210
}
202211

203212
private function fullyQualifyModelReference(string $model_name)

tests/Feature/Generators/FactoryGeneratorTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use Blueprint\Blueprint;
66
use Blueprint\Generators\FactoryGenerator;
77
use Blueprint\Tree;
8-
use Illuminate\Support\Facades\App;
98
use Tests\TestCase;
109

1110
/**
@@ -215,6 +214,7 @@ public function modelTreeDataProvider()
215214
['drafts/resource-statements.yaml', 'database/factories/UserFactory.php', 'factories/resource-statements.php'],
216215
['drafts/factory-smallint-and-tinyint.yaml', 'database/factories/ModelFactory.php', 'factories/factory-smallint-and-tinyint.php'],
217216
['drafts/all-column-types.yaml', 'database/factories/AllTypeFactory.php', 'factories/all-column-types.php'],
217+
['drafts/shorthands.yaml', 'database/factories/NameFactory.php', 'factories/shorthands.php'],
218218
];
219219
}
220220
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Database\Factories;
4+
5+
use Illuminate\Database\Eloquent\Factories\Factory;
6+
use Illuminate\Support\Str;
7+
use App\Models\Name;
8+
9+
class NameFactory extends Factory
10+
{
11+
/**
12+
* The name of the factory's corresponding model.
13+
*
14+
* @var string
15+
*/
16+
protected $model = Name::class;
17+
18+
/**
19+
* Define the model's default state.
20+
*
21+
* @return array
22+
*/
23+
public function definition()
24+
{
25+
return [
26+
//
27+
];
28+
}
29+
}

0 commit comments

Comments
 (0)