Skip to content

Commit

Permalink
helper to unassigned attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
sunel committed Mar 3, 2019
1 parent 93e7f65 commit d6267d2
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Eav;

use DB;
use Illuminate\Support\Str;
use Illuminate\Database\Eloquent\Model;

Expand Down Expand Up @@ -197,6 +198,25 @@ public function attributes()
return $this->hasMany(Attribute::class, 'entity_id');
}

/**
* Define a one-to-many relationship for attribute.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function unassignedAttributes($set = null)
{
return $this->hasMany(Attribute::class, 'entity_id')
->whereNotExists(function ($query) use ($set) {
$query->select(DB::raw(1))
->from('entity_attributes')
->whereRaw('attributes.attribute_id = entity_attributes.attribute_id')
->whereRaw('attributes.entity_id = entity_attributes.entity_id');

if(!is_null($set)) {
$query->whereRaw("entity_attributes.attribute_set_id = {$set}");
}
});
}

/**
* Find the entity by code.
Expand Down

0 comments on commit d6267d2

Please sign in to comment.