Skip to content

Commit 5a8585a

Browse files
authored
Fix isRelation() failing to check an Attribute (#40967)
1 parent 538732d commit 5a8585a

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,10 @@ public function getRelationValue($key)
499499
*/
500500
public function isRelation($key)
501501
{
502+
if ($this->hasAttributeMutator($key)) {
503+
return false;
504+
}
505+
502506
return method_exists($this, $key) ||
503507
(static::$relationResolvers[get_class($this)][$key] ?? null);
504508
}

tests/Database/DatabaseEloquentRelationTest.php

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

55
use Exception;
66
use Illuminate\Database\Eloquent\Builder;
7+
use Illuminate\Database\Eloquent\Casts\Attribute;
78
use Illuminate\Database\Eloquent\Collection;
89
use Illuminate\Database\Eloquent\Model;
910
use Illuminate\Database\Eloquent\Relations\HasOne;
@@ -281,6 +282,14 @@ public function testRelationResolvers()
281282
$this->assertInstanceOf(EloquentResolverRelationStub::class, $model->customer());
282283
$this->assertSame(['key' => 'value'], $model->customer);
283284
}
285+
286+
public function testIsRelationIgnoresAttribute()
287+
{
288+
$model = new EloquentRelationAndAtrributeModelStub;
289+
290+
$this->assertTrue($model->isRelation('parent'));
291+
$this->assertFalse($model->isRelation('field'));
292+
}
284293
}
285294

286295
class EloquentRelationResetModelStub extends Model
@@ -351,3 +360,25 @@ public function getResults()
351360
return ['key' => 'value'];
352361
}
353362
}
363+
364+
class EloquentRelationAndAtrributeModelStub extends Model
365+
{
366+
protected $table = 'one_more_table';
367+
368+
public function field(): Attribute
369+
{
370+
return new Attribute(
371+
function ($value) {
372+
return $value;
373+
},
374+
function ($value) {
375+
return $value;
376+
},
377+
);
378+
}
379+
380+
public function parent()
381+
{
382+
return $this->belongsTo(self::class);
383+
}
384+
}

0 commit comments

Comments
 (0)