Skip to content

Commit 7c8853d

Browse files
author
Ni Nelli
committed
feat: add relations return type
refs: #204
1 parent 16d5d20 commit 7c8853d

File tree

7 files changed

+22
-10
lines changed

7 files changed

+22
-10
lines changed

src/Generators/ModelGenerator.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ public function prepareRelatedModels(): void
8787
'entity' => $this->model,
8888
]);
8989

90+
$this->insertImport($content, 'Illuminate\Database\Eloquent\Relations\\' . Str::ucfirst($types[$type]));
91+
9092
$fixedContent = preg_replace('/\}$/', "\n {$newRelation}\n}", $content);
9193

9294
$this->saveClass('models', $relation, $fixedContent);
@@ -169,11 +171,13 @@ protected function getImportedRelations(): array
169171
{
170172
$result = [];
171173

172-
foreach ($this->relations as $relations) {
174+
foreach ($this->relations as $relationName => $relations) {
173175
foreach ($relations as $relation) {
174176
if ($this->shouldImportRelation($relation)) {
175177
$result[] = $this->generateClassNamespace($relation);
176178
}
179+
180+
$result[] = 'Illuminate\Database\Eloquent\Relations\\' . Str::ucfirst($relationName);
177181
}
178182
}
179183

stubs/relation.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
public function {{ $name }}()
1+
public function {{ $name }}(): {{ Str::ucfirst($type) }}
22
{
33
return $this->{{ $type }}({{ $entity }}::class);
44
}

tests/fixtures/ModelGeneratorTest/comment_relation_model.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace RonasIT\Support\Tests\Support\Models;
44

5+
use Illuminate\Database\Eloquent\Relations\BelongsTo;
56
use Illuminate\Database\Eloquent\Model;
67
use RonasIT\Support\Traits\ModelTrait;
78

@@ -23,7 +24,7 @@ public function some_relation()
2324
{
2425
}
2526

26-
public function post()
27+
public function post(): BelongsTo
2728
{
2829
return $this->belongsTo(Post::class);
2930
}

tests/fixtures/ModelGeneratorTest/new_model.php

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

55
use Illuminate\Database\Eloquent\Model;
66
use RonasIT\Support\Traits\ModelTrait;
7+
use Illuminate\Database\Eloquent\Relations\HasOne;
8+
use Illuminate\Database\Eloquent\Relations\HasMany;
79
use Carbon\Carbon;
810

911
/**
@@ -53,12 +55,12 @@ class Post extends Model
5355
'meta' => 'array',
5456
];
5557

56-
public function comment()
58+
public function comment(): HasOne
5759
{
5860
return $this->hasOne(Comment::class);
5961
}
6062

61-
public function users()
63+
public function users(): HasMany
6264
{
6365
return $this->hasMany(User::class);
6466
}

tests/fixtures/ModelGeneratorTest/new_model_with_many_relations.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
use Illuminate\Database\Eloquent\Model;
66
use RonasIT\Support\Traits\ModelTrait;
77
use App\Models\User;
8+
use Illuminate\Database\Eloquent\Relations\HasMany;
9+
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
810

911
//TODO: add @property annotation for each model's field
1012
/**
@@ -18,12 +20,12 @@ class Post extends Model
1820

1921
protected $hidden = ['pivot'];
2022

21-
public function users()
23+
public function users(): HasMany
2224
{
2325
return $this->hasMany(User::class);
2426
}
2527

26-
public function users()
28+
public function users(): BelongsToMany
2729
{
2830
return $this->belongsToMany(User::class);
2931
}

tests/fixtures/ModelGeneratorTest/new_model_with_subfolers_relations.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Illuminate\Database\Eloquent\Model;
66
use RonasIT\Support\Traits\ModelTrait;
77
use App\Models\Forum\Author;
8+
use Illuminate\Database\Eloquent\Relations\HasMany;
89

910
/**
1011
* @property string $title
@@ -19,7 +20,7 @@ class Post extends Model
1920

2021
protected $hidden = ['pivot'];
2122

22-
public function authors()
23+
public function authors(): HasMany
2324
{
2425
return $this->hasMany(Author::class);
2526
}

tests/fixtures/ModelGeneratorTest/new_subfolders_model.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
use Illuminate\Database\Eloquent\Model;
66
use RonasIT\Support\Traits\ModelTrait;
77
use App\Models\Comment;
8+
use Illuminate\Database\Eloquent\Relations\HasOne;
89
use App\Models\User;
10+
use Illuminate\Database\Eloquent\Relations\HasMany;
911
use Carbon\Carbon;
1012

1113
/**
@@ -55,12 +57,12 @@ class Post extends Model
5557
'meta' => 'array',
5658
];
5759

58-
public function comment()
60+
public function comment(): HasOne
5961
{
6062
return $this->hasOne(Comment::class);
6163
}
6264

63-
public function users()
65+
public function users(): HasMany
6466
{
6567
return $this->hasMany(User::class);
6668
}

0 commit comments

Comments
 (0)