Skip to content

Commit 92e4393

Browse files
committed
Fix EloquentResource using incorrect relation name when setting value
1 parent c4a5647 commit 92e4393

File tree

1 file changed

+20
-14
lines changed

1 file changed

+20
-14
lines changed

src/Laravel/EloquentResource.php

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,10 @@ protected function getAttributeValue(Model $model, Field $field, Context $contex
5252

5353
protected function getRelationshipValue(Model $model, Relationship $field, Context $context)
5454
{
55-
$property = $field->property ?: $field->name;
55+
$method = $this->method($field);
5656

57-
if (method_exists($model, $property)) {
58-
$relation = $model->$property();
57+
if (method_exists($model, $method)) {
58+
$relation = $model->$method();
5959

6060
// If this is a belongs-to relationship, and we only need to get the ID
6161
// 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
7070
return null;
7171
}
7272

73-
EloquentBuffer::add($model, $property);
73+
EloquentBuffer::add($model, $method);
7474

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);
7777

78-
$data = $model->getRelation($property);
78+
$data = $model->getRelation($method);
7979

8080
return $data instanceof Collection ? $data->all() : $data;
8181
};
@@ -122,10 +122,9 @@ public function find(string $id, Context $context): ?object
122122

123123
public function setValue(object $model, Field $field, mixed $value, Context $context): void
124124
{
125-
$property = $this->property($field);
126-
127125
if ($field instanceof Relationship) {
128-
$relation = $model->$property();
126+
$method = $this->method($field);
127+
$relation = $model->$method();
129128

130129
// If this is a belongs-to relationship, then the ID is stored on the
131130
// model itself, so we can set it here.
@@ -136,15 +135,14 @@ public function setValue(object $model, Field $field, mixed $value, Context $con
136135
return;
137136
}
138137

139-
$model->setAttribute($property, $value);
138+
$model->setAttribute($this->property($field), $value);
140139
}
141140

142141
public function saveValue(object $model, Field $field, mixed $value, Context $context): void
143142
{
144-
$property = $this->property($field);
145-
146143
if ($field instanceof ToMany) {
147-
$relation = $model->$property();
144+
$method = $this->method($field);
145+
$relation = $model->$method();
148146

149147
if ($relation instanceof BelongsToMany) {
150148
$relation->sync(new Collection($value));
@@ -215,4 +213,12 @@ protected function property(Field $field): string
215213
{
216214
return $field->property ?: Str::snake($field->name);
217215
}
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+
}
218224
}

0 commit comments

Comments
 (0)