Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.x] Fix isRelation() failing to check an Attribute #40967

Merged
merged 1 commit into from
Feb 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}