Skip to content

Commit

Permalink
Added scopes to trait
Browse files Browse the repository at this point in the history
  • Loading branch information
davidwesdijk committed Jul 21, 2020
1 parent 51ac330 commit 3541aa5
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/Scopes/EntityAttributeValueScopes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace DavidWesdijk\LaravelPolymorphicEav\Scopes;

use DavidWesdijk\LaravelPolymorphicEav\EntityAttribute;
use Illuminate\Database\Eloquent\Builder;

/**
* Trait EntityAttributeValueScopes
* @package DavidWesdijk\LaravelPolymorphicEav\Scopes
*
* @method static hasAttribute(EntityAttribute|int $attribute) Get instances with a given attribute
*/
trait EntityAttributeValueScopes
{
/**
* Get instances with a given attribute
*
* @param Builder $query
* @param $attribute
*
* @return Builder
*/
public function scopeHasAttribute(Builder $query, $attribute): Builder
{
$attributeId = ($attribute instanceof EntityAttribute) ? $attribute->id : $attribute;

return $query->whereHas(static::$relationName, function (Builder $query) use ($attributeId) {
$query->where('entity_attribute_id', $attributeId);
});
}
}
7 changes: 7 additions & 0 deletions src/Traits/HasEntityAttributeValues.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,16 @@

use DavidWesdijk\LaravelPolymorphicEav\EntityAttributeValue;
use DavidWesdijk\LaravelPolymorphicEav\Resolvers\GroupResolver;
use DavidWesdijk\LaravelPolymorphicEav\Scopes\EntityAttributeValueScopes;

/**
* Trait HasEntityAttributeValues
* @package DavidWesdijk\LaravelPolymorphicEav\Traits
*/
trait HasEntityAttributeValues
{
use EntityAttributeValueScopes;

/**
* @var string
*/
Expand Down

0 comments on commit 3541aa5

Please sign in to comment.