Skip to content

Commit 4070965

Browse files
committed
Fixes #32 Attaches relationships to models in factory definitions
1 parent 6ca97cd commit 4070965

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

src/Generators/FactoryGenerator.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Blueprint\Contracts\Generator;
66
use Blueprint\Models\Model;
7+
use Illuminate\Support\Str;
78

89
class FactoryGenerator implements Generator
910
{
@@ -61,10 +62,19 @@ protected function buildDefinition(Model $model)
6162
continue;
6263
}
6364

64-
$definition .= self::INDENT . "'{$column->name()}' => ";
65-
$faker = $this->fakerData($column->name()) ?? $this->fakerDataType($column->dataType());
66-
$definition .= '$faker->' . $faker;
67-
$definition .= ',' . PHP_EOL;
65+
if ($column->dataType() === 'id') {
66+
$name = Str::substr($column->name(), 0, -3);
67+
$class = Str::studly($column->attributes()[0] ?? $name);
68+
69+
$definition .= self::INDENT . "'{$column->name()}' => ";
70+
$definition .= sprintf("factory(\App\%s::class)", $class);
71+
$definition .= ',' . PHP_EOL;
72+
} else {
73+
$definition .= self::INDENT . "'{$column->name()}' => ";
74+
$faker = $this->fakerData($column->name()) ?? $this->fakerDataType($column->dataType());
75+
$definition .= '$faker->' . $faker;
76+
$definition .= ',' . PHP_EOL;
77+
}
6878
}
6979

7080
return trim($definition);

0 commit comments

Comments
 (0)