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

Add hasAttribute method to Eloquent Models #44717

Closed
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
39 changes: 33 additions & 6 deletions src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,37 @@ protected function addMutatedAttributesToArray(array $attributes, array $mutated
return $attributes;
}

/**
* Check if model has a given attribute.
*
* @param string $key
* @return bool
*/
public function hasAttribute($key)
{
if (array_key_exists($key, $this->attributes)) {
return true;
}

if (array_key_exists($key, $this->casts)) {
return true;
}

if ($this->hasGetMutator($key)) {
return true;
}

if ($this->hasAttributeMutator($key)) {
return true;
}

if ($this->isClassCastable($key)) {
return true;
}

return false;
}

/**
* Add the casted attributes to the attributes array.
*
Expand Down Expand Up @@ -432,13 +463,9 @@ public function getAttribute($key)
}

// If the attribute exists in the attribute array or has a "get" mutator we will
// get the attribute's value. Otherwise, we will proceed as if the developers
// return the attribute's value. Otherwise, we will proceed as if the developers
// are asking for a relationship's value. This covers both types of values.
if (array_key_exists($key, $this->attributes) ||
array_key_exists($key, $this->casts) ||
$this->hasGetMutator($key) ||
$this->hasAttributeMutator($key) ||
$this->isClassCastable($key)) {
if ($this->hasAttribute($key)) {
return $this->getAttributeValue($key);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public function testModelsAreProperlyMatchedToParents()
{
$relation = $this->getRelation();
$model1 = m::mock(Model::class);
$model1->shouldReceive('hasAttribute')->passthru();
$model1->shouldReceive('getAttribute')->with('parent_key')->andReturn(1);
$model1->shouldReceive('getAttribute')->with('foo')->passthru();
$model1->shouldReceive('hasGetMutator')->andReturn(false);
Expand All @@ -28,6 +29,7 @@ public function testModelsAreProperlyMatchedToParents()
$model1->shouldReceive('getRelationValue', 'relationLoaded', 'setRelation', 'isRelation')->passthru();

$model2 = m::mock(Model::class);
$model2->shouldReceive('hasAttribute')->passthru();
$model2->shouldReceive('getAttribute')->with('parent_key')->andReturn(2);
$model2->shouldReceive('getAttribute')->with('foo')->passthru();
$model2->shouldReceive('hasGetMutator')->andReturn(false);
Expand Down
58 changes: 58 additions & 0 deletions tests/Database/DatabaseEloquentModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Illuminate\Database\Eloquent\Casts\AsEncryptedArrayObject;
use Illuminate\Database\Eloquent\Casts\AsEncryptedCollection;
use Illuminate\Database\Eloquent\Casts\AsStringable;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\JsonEncodingException;
use Illuminate\Database\Eloquent\MassAssignmentException;
Expand Down Expand Up @@ -373,6 +374,49 @@ public function testOnly()
$this->assertEquals(['first_name' => 'taylor', 'last_name' => 'otwell'], $model->only(['first_name', 'last_name']));
}

public function testHasAttributeWithAttributes()
{
$model = new EloquentModelStub;
$model->first_name = 'Taylor';
$model->last_name = 'Otwell';

$this->assertTrue($model->hasAttribute('first_name'));
$this->assertTrue($model->hasAttribute('last_name'));
$this->assertFalse($model->hasAttribute('project'));
}

public function testHasAttributeWithCasts()
{
$model = new EloquentModelStub;

$this->assertTrue($model->hasAttribute('castedFloat'));
$this->assertFalse($model->hasAttribute('project'));
}

public function testHasAttributeWithGetMutators()
{
$model = new EloquentModelGetMutatorsStub();

$this->assertTrue($model->hasAttribute('first_name'));
$this->assertFalse($model->hasAttribute('project'));
}

public function testHasAttributeWithAttributeMutators()
{
$model = new EloquentModelWithAttributeMutator();

$this->assertTrue($model->hasAttribute('first_name'));
$this->assertFalse($model->hasAttribute('project'));
}

public function testHasAttributeWithCastableCast()
{
$model = new EloquentModelCastingStub();

$this->assertTrue($model->hasAttribute('asarrayobjectAttribute'));
$this->assertFalse($model->hasAttribute('project'));
}

public function testNewInstanceReturnsNewInstanceWithAttributesSet()
{
$model = new EloquentModelStub;
Expand Down Expand Up @@ -3047,6 +3091,20 @@ class EloquentModelWithUpdatedAtNull extends Model
const UPDATED_AT = null;
}

class EloquentModelWithAttributeMutator extends Model
{
protected $table = 'stub';

public function firstName(): Attribute
{
return Attribute::make(function () {
return 'Taylor';
}, function ($value) {
return $value;
});
}
}

class UnsavedModel extends Model
{
protected $casts = ['name' => Uppercase::class];
Expand Down