Skip to content

Commit

Permalink
Fix isRelation() failing to check an Attribute (#40967)
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigopedra authored Feb 11, 2022
1 parent 538732d commit 5a8585a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,10 @@ public function getRelationValue($key)
*/
public function isRelation($key)
{
if ($this->hasAttributeMutator($key)) {
return false;
}

return method_exists($this, $key) ||
(static::$relationResolvers[get_class($this)][$key] ?? null);
}
Expand Down
31 changes: 31 additions & 0 deletions tests/Database/DatabaseEloquentRelationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Exception;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasOne;
Expand Down Expand Up @@ -281,6 +282,14 @@ public function testRelationResolvers()
$this->assertInstanceOf(EloquentResolverRelationStub::class, $model->customer());
$this->assertSame(['key' => 'value'], $model->customer);
}

public function testIsRelationIgnoresAttribute()
{
$model = new EloquentRelationAndAtrributeModelStub;

$this->assertTrue($model->isRelation('parent'));
$this->assertFalse($model->isRelation('field'));
}
}

class EloquentRelationResetModelStub extends Model
Expand Down Expand Up @@ -351,3 +360,25 @@ public function getResults()
return ['key' => 'value'];
}
}

class EloquentRelationAndAtrributeModelStub extends Model
{
protected $table = 'one_more_table';

public function field(): Attribute
{
return new Attribute(
function ($value) {
return $value;
},
function ($value) {
return $value;
},
);
}

public function parent()
{
return $this->belongsTo(self::class);
}
}

0 comments on commit 5a8585a

Please sign in to comment.