3
3
namespace BalajiDharma \LaravelAttributes \Traits ;
4
4
5
5
use Illuminate \Database \Eloquent \Builder ;
6
- use Illuminate \Database \Eloquent \Concerns \HasRelationships ;
7
6
use Illuminate \Database \Eloquent \Model ;
8
7
use Illuminate \Database \Eloquent \Relations \MorphMany ;
8
+ use Illuminate \Support \Collection ;
9
9
10
10
trait HasAttributable
11
11
{
12
- use HasRelationships;
13
-
14
12
/**
15
- * Get attributes.
16
- *
17
- * @return MorphMany
13
+ * Get attributes relationship.
18
14
*/
19
- public function attributes ()
15
+ public function attributes (): MorphMany
20
16
{
21
17
return $ this ->morphMany (
22
18
config ('attributes.models.attributes ' ),
23
- 'attributable ' ,
24
19
'attributable '
25
20
);
26
21
}
27
22
28
23
/**
29
- * Attach attribute.
30
- *
31
- * @return Builder|Model
24
+ * Attach a single attribute.
32
25
*/
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 ([
36
33
'data_type ' => $ data_type ,
37
- 'name ' => $ name ,
34
+ 'name ' => $ name ,
38
35
'value ' => $ value ,
39
36
'attributable_id ' => $ this ->getKey (),
40
- 'attributable ' => $ this ->getMorphClass (),
37
+ 'attributable ' => $ this ->getMorphClass (),
41
38
'weight ' => $ weight ,
42
- ];
43
-
44
- return $ this ->attributes ()->create ($ attributes );
39
+ ]);
45
40
}
46
41
47
42
/**
48
43
* Attach multiple attributes.
49
- *
50
- * @return $this
51
44
*/
52
- public function attachAttributes (array $ values )
45
+ public function attachAttributes (array $ values ): self
53
46
{
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 );
62
56
63
57
return $ this ;
64
58
}
65
59
66
60
/**
67
- * Check attribute have special value.
68
- *
69
- * @return bool
61
+ * Check if attribute has specific value.
70
62
*/
71
- public function hasAttributeValue (string $ value )
63
+ public function hasAttributeValue (string $ value ): bool
72
64
{
73
- return $ this ->getAttributeWhere ()
65
+ return $ this ->getAttributeQuery ()
74
66
->where ('value ' , $ value )
75
67
->exists ();
76
68
}
77
69
78
70
/**
79
- * Check attribute have special name.
80
- *
81
- * @return bool
71
+ * Check if attribute has specific name.
82
72
*/
83
- public function hasAttributeName (string $ name )
73
+ public function hasAttributeName (string $ name ): bool
84
74
{
85
- return $ this ->getAttributeWhere ()
75
+ return $ this ->getAttributeQuery ()
86
76
->where ('name ' , $ name )
87
77
->exists ();
88
78
}
89
79
90
80
/**
91
81
* Delete all attributes.
92
- *
93
- * @return Attributable
94
82
*/
95
- public function deleteAllAttribute ()
83
+ public function deleteAllAttributes (): self
96
84
{
97
- $ attributes = $ this ->getAttributeWhere ()->get ();
98
-
99
- foreach ($ attributes as $ attribute ) {
100
- $ attribute ->delete ();
101
- }
102
-
85
+ $ this ->getAttributeQuery ()->delete ();
103
86
return $ this ;
104
87
}
105
88
106
89
/**
107
- * Delete special attribute.
108
- *
109
- * @return int
90
+ * Delete attribute by name and value.
110
91
*/
111
- public function deleteAttribute (string $ name , string $ value )
92
+ public function deleteAttribute (string $ name , string $ value ): int
112
93
{
113
- return $ this ->getAttributeWhere ()
94
+ return $ this ->getAttributeQuery ()
114
95
->where ('name ' , $ name )
115
96
->where ('value ' , $ value )
116
97
->delete ();
117
98
}
118
99
119
100
/**
120
101
* Delete attribute by name.
121
- *
122
- * @return int
123
102
*/
124
- public function deleteAttributeByName (string $ name )
103
+ public function deleteAttributeByName (string $ name ): int
125
104
{
126
- return $ this ->getAttributeWhere ()
105
+ return $ this ->getAttributeQuery ()
127
106
->where ('name ' , $ name )
128
107
->delete ();
129
108
}
130
109
131
110
/**
132
111
* Delete attribute by value.
133
- *
134
- * @return int
135
112
*/
136
- public function deleteAttributeByValue (string $ value )
113
+ public function deleteAttributeByValue (string $ value ): int
137
114
{
138
- return $ this ->getAttributeWhere ()
115
+ return $ this ->getAttributeQuery ()
139
116
->where ('value ' , $ value )
140
117
->delete ();
141
118
}
142
119
143
120
/**
144
- * Get attribute with this (model) .
121
+ * Get base attribute query .
145
122
*/
146
- private function getAttributeWhere (): MorphMany
123
+ private function getAttributeQuery (): MorphMany
147
124
{
148
125
return $ this ->attributes ()
149
126
->where ('attributable_id ' , $ this ->getKey ())
150
127
->where ('attributable ' , $ this ->getMorphClass ());
151
128
}
152
- }
129
+ }
0 commit comments