Skip to content

Commit 848c312

Browse files
author
Ni Nelli
committed
chore: merge with origin
2 parents 46d0306 + a278498 commit 848c312

File tree

8 files changed

+24
-11
lines changed

8 files changed

+24
-11
lines changed

src/Generators/ModelGenerator.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ public function prepareRelatedModels(): void
8585
// TODO: use ronasit/larabuilder instead regexp
8686
$fixedContent = preg_replace('/\}$/', "\n {$newRelation}\n}", $content);
8787

88+
$this->insertImport($fixedContent, 'Illuminate\Database\Eloquent\Relations\\' . Str::ucfirst($types[$type]));
89+
8890
$this->insertPropertyAnnotation($fixedContent, $this->getRelationType($this->model, $types[$type]), $relationName);
8991

9092
$this->saveClass('models', $relation, $fixedContent);
@@ -157,11 +159,13 @@ protected function getImportedRelations(): array
157159
{
158160
$result = [];
159161

160-
foreach ($this->relations as $relations) {
162+
foreach ($this->relations as $relationType => $relations) {
161163
foreach ($relations as $relation) {
162164
if ($this->shouldImportRelation($relation)) {
163165
$result[] = $this->generateClassNamespace($relation);
164166
}
167+
168+
$result[] = 'Illuminate\Database\Eloquent\Relations\\' . Str::ucfirst($relationType);
165169
}
166170
}
167171

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

@@ -26,7 +27,7 @@ public function some_relation()
2627
{
2728
}
2829

29-
public function post()
30+
public function post(): BelongsTo
3031
{
3132
return $this->belongsTo(Post::class);
3233
}

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
use Illuminate\Database\Eloquent\Collection;
911

@@ -56,12 +58,12 @@ class Post extends Model
5658
'meta' => 'array',
5759
];
5860

59-
public function comment()
61+
public function comment(): HasOne
6062
{
6163
return $this->hasOne(Comment::class);
6264
}
6365

64-
public function users()
66+
public function users(): HasMany
6567
{
6668
return $this->hasMany(User::class);
6769
}

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
use Illuminate\Database\Eloquent\Collection;
911

1012
/**
@@ -19,12 +21,12 @@ class Post extends Model
1921

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

22-
public function users()
24+
public function users(): HasMany
2325
{
2426
return $this->hasMany(User::class);
2527
}
2628

27-
public function users()
29+
public function users(): BelongsToMany
2830
{
2931
return $this->belongsToMany(User::class);
3032
}

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
use Illuminate\Database\Eloquent\Collection;
910

1011
/**
@@ -21,7 +22,7 @@ class Post extends Model
2122

2223
protected $hidden = ['pivot'];
2324

24-
public function authors()
25+
public function authors(): HasMany
2526
{
2627
return $this->hasMany(Author::class);
2728
}

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
use Illuminate\Database\Eloquent\Collection;
1113

@@ -58,12 +60,12 @@ class Post extends Model
5860
'meta' => 'array',
5961
];
6062

61-
public function comment()
63+
public function comment(): HasOne
6264
{
6365
return $this->hasOne(Comment::class);
6466
}
6567

66-
public function users()
68+
public function users(): HasMany
6769
{
6870
return $this->hasMany(User::class);
6971
}

tests/fixtures/ModelGeneratorTest/related_model_with_property.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace RonasIT\Support\Tests\Support\Models;
44

55
use Illuminate\Database\Eloquent\Collection;
6+
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
67
use Illuminate\Database\Eloquent\Model;
78
use RonasIT\Support\Traits\ModelTrait;
89

@@ -27,7 +28,7 @@ public function some_relation()
2728
{
2829
}
2930

30-
public function categories()
31+
public function categories(): BelongsToMany
3132
{
3233
return $this->belongsToMany(Category::class);
3334
}

0 commit comments

Comments
 (0)