Skip to content

Commit 9c06850

Browse files
authored
Do not use FQCN for model relationships in same namespace (#524)
1 parent 464bc7d commit 9c06850

21 files changed

+38
-37
lines changed

src/Generators/ModelGenerator.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ protected function buildRelationships(Model $model)
209209
}
210210

211211
$fqcn = Str::startsWith($fqcn, '\\') ? $fqcn : '\\' . $fqcn;
212+
$fqcn = Str::is($fqcn, "\\{$model->fullyQualifiedNamespace()}\\{$class_name}") ? $class_name : $fqcn;
212213

213214
if ($type === 'morphTo') {
214215
$relationship = sprintf('$this->%s()', $type);

tests/fixtures/models/alias-relationships.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,16 @@ class Salesman extends Model
2929

3030
public function lead()
3131
{
32-
return $this->hasOne(\App\User::class);
32+
return $this->hasOne(User::class);
3333
}
3434

3535
public function methodNames()
3636
{
37-
return $this->hasMany(\App\ClassName::class);
37+
return $this->hasMany(ClassName::class);
3838
}
3939

4040
public function methodName()
4141
{
42-
return $this->belongsTo(\App\ClassName::class);
42+
return $this->belongsTo(ClassName::class);
4343
}
4444
}

tests/fixtures/models/certificate-pascal-case-example.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,6 @@ class Certificate extends Model
3636

3737
public function certificateType()
3838
{
39-
return $this->belongsTo(\App\CertificateType::class);
39+
return $this->belongsTo(CertificateType::class);
4040
}
4141
}

tests/fixtures/models/certificate-type-pascal-case-example.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,6 @@ class CertificateType extends Model
2929

3030
public function certificates()
3131
{
32-
return $this->hasMany(\App\Certificate::class);
32+
return $this->hasMany(Certificate::class);
3333
}
3434
}

tests/fixtures/models/custom-pivot-table-name.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,6 @@ class User extends Model
3838

3939
public function accounts()
4040
{
41-
return $this->belongsToMany(\App\Account::class, 'test');
41+
return $this->belongsToMany(Account::class, 'test');
4242
}
4343
}

tests/fixtures/models/foreign-key-shorthand-phpdoc.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,22 +45,22 @@ class Comment extends Model
4545
*/
4646
public function post()
4747
{
48-
return $this->belongsTo(\App\Post::class);
48+
return $this->belongsTo(Post::class);
4949
}
5050

5151
/**
5252
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
5353
*/
5454
public function author()
5555
{
56-
return $this->belongsTo(\App\User::class);
56+
return $this->belongsTo(User::class);
5757
}
5858

5959
/**
6060
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
6161
*/
6262
public function country()
6363
{
64-
return $this->belongsTo(\App\Country::class, 'ccid', 'code');
64+
return $this->belongsTo(Country::class, 'ccid', 'code');
6565
}
6666
}

tests/fixtures/models/model-configured.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ class Comment extends Model
3232

3333
public function post()
3434
{
35-
return $this->belongsTo(\Some\App\Models\Post::class);
35+
return $this->belongsTo(Post::class);
3636
}
3737

3838
public function author()
3939
{
40-
return $this->belongsTo(\Some\App\Models\User::class);
40+
return $this->belongsTo(User::class);
4141
}
4242
}

tests/fixtures/models/model-relationships-morphone-morphmany-with-fqn.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,6 @@ public function lines()
4545

4646
public function user()
4747
{
48-
return $this->belongsTo(\App\User::class);
48+
return $this->belongsTo(User::class);
4949
}
5050
}

tests/fixtures/models/model-relationships-with-full-namespace.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,11 @@ public function transaction()
5151

5252
public function user()
5353
{
54-
return $this->belongsTo(\App\User::class);
54+
return $this->belongsTo(User::class);
5555
}
5656

5757
public function product()
5858
{
59-
return $this->belongsTo(\App\Product::class);
59+
return $this->belongsTo(Product::class);
6060
}
6161
}

tests/fixtures/models/model-relationships.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,31 +31,31 @@ class Subscription extends Model
3131

3232
public function teams()
3333
{
34-
return $this->belongsToMany(\App\Team::class);
34+
return $this->belongsToMany(Team::class);
3535
}
3636

3737
public function orders()
3838
{
39-
return $this->hasMany(\App\Order::class);
39+
return $this->hasMany(Order::class);
4040
}
4141

4242
public function duration()
4343
{
44-
return $this->hasOne(\App\Duration::class);
44+
return $this->hasOne(Duration::class);
4545
}
4646

4747
public function transaction()
4848
{
49-
return $this->hasOne(\App\Transaction::class);
49+
return $this->hasOne(Transaction::class);
5050
}
5151

5252
public function user()
5353
{
54-
return $this->belongsTo(\App\User::class);
54+
return $this->belongsTo(User::class);
5555
}
5656

5757
public function product()
5858
{
59-
return $this->belongsTo(\App\Product::class);
59+
return $this->belongsTo(Product::class);
6060
}
6161
}

0 commit comments

Comments
 (0)