@@ -52,10 +52,10 @@ protected function getAttributeValue(Model $model, Field $field, Context $contex
52
52
53
53
protected function getRelationshipValue (Model $ model , Relationship $ field , Context $ context )
54
54
{
55
- $ property = $ field -> property ?: $ field-> name ;
55
+ $ method = $ this -> method ( $ field) ;
56
56
57
- if (method_exists ($ model , $ property )) {
58
- $ relation = $ model ->$ property ();
57
+ if (method_exists ($ model , $ method )) {
58
+ $ relation = $ model ->$ method ();
59
59
60
60
// If this is a belongs-to relationship, and we only need to get the ID
61
61
// for linkage, then we don't have to actually load the relation because
@@ -70,12 +70,12 @@ protected function getRelationshipValue(Model $model, Relationship $field, Conte
70
70
return null ;
71
71
}
72
72
73
- EloquentBuffer::add ($ model , $ property );
73
+ EloquentBuffer::add ($ model , $ method );
74
74
75
- return function () use ($ model , $ property , $ field , $ context ) {
76
- EloquentBuffer::load ($ model , $ property , $ field , $ context );
75
+ return function () use ($ model , $ method , $ field , $ context ) {
76
+ EloquentBuffer::load ($ model , $ method , $ field , $ context );
77
77
78
- $ data = $ model ->getRelation ($ property );
78
+ $ data = $ model ->getRelation ($ method );
79
79
80
80
return $ data instanceof Collection ? $ data ->all () : $ data ;
81
81
};
@@ -122,10 +122,9 @@ public function find(string $id, Context $context): ?object
122
122
123
123
public function setValue (object $ model , Field $ field , mixed $ value , Context $ context ): void
124
124
{
125
- $ property = $ this ->property ($ field );
126
-
127
125
if ($ field instanceof Relationship) {
128
- $ relation = $ model ->$ property ();
126
+ $ method = $ this ->method ($ field );
127
+ $ relation = $ model ->$ method ();
129
128
130
129
// If this is a belongs-to relationship, then the ID is stored on the
131
130
// model itself, so we can set it here.
@@ -136,15 +135,14 @@ public function setValue(object $model, Field $field, mixed $value, Context $con
136
135
return ;
137
136
}
138
137
139
- $ model ->setAttribute ($ property , $ value );
138
+ $ model ->setAttribute ($ this -> property ( $ field ) , $ value );
140
139
}
141
140
142
141
public function saveValue (object $ model , Field $ field , mixed $ value , Context $ context ): void
143
142
{
144
- $ property = $ this ->property ($ field );
145
-
146
143
if ($ field instanceof ToMany) {
147
- $ relation = $ model ->$ property ();
144
+ $ method = $ this ->method ($ field );
145
+ $ relation = $ model ->$ method ();
148
146
149
147
if ($ relation instanceof BelongsToMany) {
150
148
$ relation ->sync (new Collection ($ value ));
@@ -215,4 +213,12 @@ protected function property(Field $field): string
215
213
{
216
214
return $ field ->property ?: Str::snake ($ field ->name );
217
215
}
216
+
217
+ /**
218
+ * Get the model method that a field represents.
219
+ */
220
+ protected function method (Field $ field ): string
221
+ {
222
+ return $ field ->property ?: $ field ->name ;
223
+ }
218
224
}
0 commit comments