-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
51ac330
commit 3541aa5
Showing
2 changed files
with
39 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters