Skip to content

Commit 5696378

Browse files
committed
Added Attributable Trait
1 parent 01ef3d2 commit 5696378

File tree

1 file changed

+42
-65
lines changed

1 file changed

+42
-65
lines changed

src/Traits/HasAttributable.php

+42-65
Original file line numberDiff line numberDiff line change
@@ -3,150 +3,127 @@
33
namespace BalajiDharma\LaravelAttributes\Traits;
44

55
use Illuminate\Database\Eloquent\Builder;
6-
use Illuminate\Database\Eloquent\Concerns\HasRelationships;
76
use Illuminate\Database\Eloquent\Model;
87
use Illuminate\Database\Eloquent\Relations\MorphMany;
8+
use Illuminate\Support\Collection;
99

1010
trait HasAttributable
1111
{
12-
use HasRelationships;
13-
1412
/**
15-
* Get attributes.
16-
*
17-
* @return MorphMany
13+
* Get attributes relationship.
1814
*/
19-
public function attributes()
15+
public function attributes(): MorphMany
2016
{
2117
return $this->morphMany(
2218
config('attributes.models.attributes'),
23-
'attributable',
2419
'attributable'
2520
);
2621
}
2722

2823
/**
29-
* Attach attribute.
30-
*
31-
* @return Builder|Model
24+
* Attach a single attribute.
3225
*/
33-
public function attachAttribute(string $name, string $value, ?string $data_type = 'string', ?int $weight = 0)
34-
{
35-
$attributes = [
26+
public function attachAttribute(
27+
string $name,
28+
string $value,
29+
?string $data_type = 'string',
30+
?int $weight = 0
31+
): Model {
32+
return $this->attributes()->create([
3633
'data_type' => $data_type,
37-
'name' => $name,
34+
'name' => $name,
3835
'value' => $value,
3936
'attributable_id' => $this->getKey(),
40-
'attributable' => $this->getMorphClass(),
37+
'attributable' => $this->getMorphClass(),
4138
'weight' => $weight,
42-
];
43-
44-
return $this->attributes()->create($attributes);
39+
]);
4540
}
4641

4742
/**
4843
* Attach multiple attributes.
49-
*
50-
* @return $this
5144
*/
52-
public function attachAttributes(array $values)
45+
public function attachAttributes(array $values): self
5346
{
54-
$weight = 1;
55-
foreach ($values as $value) {
56-
$value['attributable_id'] = $this->getKey();
57-
$value['attributable'] = $this->getMorphClass();
58-
$value['weight'] = $value['weight'] ?? $weight;
59-
$this->attributes()->create($value);
60-
$weight++;
61-
}
47+
$attributes = collect($values)->map(function ($value, $index) {
48+
return array_merge($value, [
49+
'attributable_id' => $this->getKey(),
50+
'attributable' => $this->getMorphClass(),
51+
'weight' => $value['weight'] ?? $index + 1,
52+
]);
53+
})->all();
54+
55+
$this->attributes()->createMany($attributes);
6256

6357
return $this;
6458
}
6559

6660
/**
67-
* Check attribute have special value.
68-
*
69-
* @return bool
61+
* Check if attribute has specific value.
7062
*/
71-
public function hasAttributeValue(string $value)
63+
public function hasAttributeValue(string $value): bool
7264
{
73-
return $this->getAttributeWhere()
65+
return $this->getAttributeQuery()
7466
->where('value', $value)
7567
->exists();
7668
}
7769

7870
/**
79-
* Check attribute have special name.
80-
*
81-
* @return bool
71+
* Check if attribute has specific name.
8272
*/
83-
public function hasAttributeName(string $name)
73+
public function hasAttributeName(string $name): bool
8474
{
85-
return $this->getAttributeWhere()
75+
return $this->getAttributeQuery()
8676
->where('name', $name)
8777
->exists();
8878
}
8979

9080
/**
9181
* Delete all attributes.
92-
*
93-
* @return Attributable
9482
*/
95-
public function deleteAllAttribute()
83+
public function deleteAllAttributes(): self
9684
{
97-
$attributes = $this->getAttributeWhere()->get();
98-
99-
foreach ($attributes as $attribute) {
100-
$attribute->delete();
101-
}
102-
85+
$this->getAttributeQuery()->delete();
10386
return $this;
10487
}
10588

10689
/**
107-
* Delete special attribute.
108-
*
109-
* @return int
90+
* Delete attribute by name and value.
11091
*/
111-
public function deleteAttribute(string $name, string $value)
92+
public function deleteAttribute(string $name, string $value): int
11293
{
113-
return $this->getAttributeWhere()
94+
return $this->getAttributeQuery()
11495
->where('name', $name)
11596
->where('value', $value)
11697
->delete();
11798
}
11899

119100
/**
120101
* Delete attribute by name.
121-
*
122-
* @return int
123102
*/
124-
public function deleteAttributeByName(string $name)
103+
public function deleteAttributeByName(string $name): int
125104
{
126-
return $this->getAttributeWhere()
105+
return $this->getAttributeQuery()
127106
->where('name', $name)
128107
->delete();
129108
}
130109

131110
/**
132111
* Delete attribute by value.
133-
*
134-
* @return int
135112
*/
136-
public function deleteAttributeByValue(string $value)
113+
public function deleteAttributeByValue(string $value): int
137114
{
138-
return $this->getAttributeWhere()
115+
return $this->getAttributeQuery()
139116
->where('value', $value)
140117
->delete();
141118
}
142119

143120
/**
144-
* Get attribute with this (model).
121+
* Get base attribute query.
145122
*/
146-
private function getAttributeWhere(): MorphMany
123+
private function getAttributeQuery(): MorphMany
147124
{
148125
return $this->attributes()
149126
->where('attributable_id', $this->getKey())
150127
->where('attributable', $this->getMorphClass());
151128
}
152-
}
129+
}

0 commit comments

Comments
 (0)