Skip to content

Commit 8a2b7d3

Browse files
committed
Fixes #47
1 parent 3974db7 commit 8a2b7d3

File tree

7 files changed

+69
-4
lines changed

7 files changed

+69
-4
lines changed

src/Generators/FactoryGenerator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ protected function buildDefinition(Model $model)
6363
}
6464

6565
if ($column->dataType() === 'id') {
66-
$name = Str::substr($column->name(), 0, -3);
66+
$name = Str::beforeLast($column->name(), '_id');
6767
$class = Str::studly($column->attributes()[0] ?? $name);
6868

6969
$definition .= self::INDENT . "'{$column->name()}' => ";

src/Generators/ModelGenerator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ private function buildProperties(Model $model)
7979
private function buildRelationships(Model $model)
8080
{
8181
$columns = array_filter($model->columns(), function (Column $column) {
82-
return Str::endsWith($column->name(), '_id');
82+
return $column->name() !== 'id' && $column->dataType() === 'id';
8383
});
8484

8585
if (empty($columns)) {
@@ -91,7 +91,7 @@ private function buildRelationships(Model $model)
9191

9292
/** @var Column $column */
9393
foreach ($columns as $column) {
94-
$name = Str::substr($column->name(), 0, -3);
94+
$name = Str::beforeLast($column->name(), '_id');
9595
$class = Str::studly($column->attributes()[0] ?? $name);
9696
$relationship = sprintf("\$this->belongsTo(\App\%s::class)", $class);
9797

tests/Feature/Generator/FactoryGeneratorTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
use Blueprint\Generators\FactoryGenerator;
77
use Tests\TestCase;
88

9+
/**
10+
* @see FactoryGenerator
11+
*/
912
class FactoryGeneratorTest extends TestCase
1013
{
1114
private $blueprint;
@@ -64,7 +67,8 @@ public function modelTreeDataProvider()
6467
{
6568
return [
6669
['definitions/post.bp', 'database/factories/PostFactory.php', 'factories/post.php'],
67-
['definitions/team.bp', 'database/factories/TeamFactory.php', 'factories/team.php']
70+
['definitions/team.bp', 'database/factories/TeamFactory.php', 'factories/team.php'],
71+
['definitions/unconventional.bp', 'database/factories/TeamFactory.php', 'factories/unconventional.php']
6872
];
6973
}
7074
}

tests/Feature/Generator/ModelGeneratorTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ public function modelTreeDataProvider()
9191
['definitions/with-timezones.bp', 'app/Comment.php', 'models/comment.php'],
9292
['definitions/soft-deletes.bp', 'app/Comment.php', 'models/soft-deletes.php'],
9393
['definitions/relationships.bp', 'app/Comment.php', 'models/relationships.php'],
94+
['definitions/unconventional.bp', 'app/Team.php', 'models/unconventional.php'],
9495
];
9596
}
9697
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
models:
2+
Team:
3+
name: string
4+
owner: id
5+
manager: id:user
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
/** @var \Illuminate\Database\Eloquent\Factory $factory */
4+
5+
use App\Team;
6+
use Faker\Generator as Faker;
7+
8+
$factory->define(Team::class, function (Faker $faker) {
9+
return [
10+
'name' => $faker->name,
11+
'owner' => factory(\App\Owner::class),
12+
'manager' => factory(\App\User::class),
13+
];
14+
});
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace App;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
7+
class Team extends Model
8+
{
9+
/**
10+
* The attributes that are mass assignable.
11+
*
12+
* @var array
13+
*/
14+
protected $fillable = [
15+
'name',
16+
'owner',
17+
'manager',
18+
];
19+
20+
/**
21+
* The attributes that should be cast to native types.
22+
*
23+
* @var array
24+
*/
25+
protected $casts = [
26+
'id' => 'integer',
27+
'owner' => 'integer',
28+
'manager' => 'integer',
29+
];
30+
31+
32+
public function owner()
33+
{
34+
return $this->belongsTo(\App\Owner::class);
35+
}
36+
37+
public function manager()
38+
{
39+
return $this->belongsTo(\App\User::class);
40+
}
41+
}

0 commit comments

Comments
 (0)