Skip to content

Commit f5b2f53

Browse files
authored
Merge pull request #585 from code16/fix-model-method-not-relation
Enhance EloquentModelUpdater to support non-relation model methods
2 parents e364d15 + 736ba12 commit f5b2f53

File tree

4 files changed

+44
-1
lines changed

4 files changed

+44
-1
lines changed

src/Form/Eloquent/EloquentModelUpdater.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
use Closure;
66
use Illuminate\Contracts\Support\Arrayable;
77
use Illuminate\Database\Eloquent\Model;
8+
use Illuminate\Database\Eloquent\Relations\Relation;
89
use ReflectionClass;
10+
use ReflectionMethod;
11+
use ReflectionNamedType;
912

1013
class EloquentModelUpdater
1114
{
@@ -73,7 +76,18 @@ protected function valuateAttribute(Model $instance, string $attribute, $value):
7376

7477
protected function isRelationship(Model $instance, string $attribute): bool
7578
{
76-
return str($attribute)->contains(':') || $instance->isRelation($attribute);
79+
if(str($attribute)->contains(':')) {
80+
return true;
81+
}
82+
83+
if($instance->isRelation($attribute)) {
84+
$returnType = (new ReflectionMethod($instance, $attribute))->getReturnType();
85+
86+
return $returnType instanceof ReflectionNamedType
87+
&& is_subclass_of($returnType->getName(), Relation::class);
88+
}
89+
90+
return false;
7791
}
7892

7993
protected function saveRelationships(Model $instance): void

tests/Fixtures/Person.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,9 @@ public function upload(): MorphOne
5555
{
5656
return $this->morphOne(SharpUploadModel::class, 'model');
5757
}
58+
59+
public function unrelated(): string
60+
{
61+
return 'unrelated';
62+
}
5863
}

tests/Pest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
$table->unsignedTinyInteger('order')->nullable();
2121
$table->unsignedInteger('partner_id')->nullable();
2222
$table->unsignedInteger('chief_id')->nullable();
23+
$table->string('unrelated')->nullable();
2324
$table->timestamps();
2425
});
2526

tests/Unit/Form/Eloquent/SharpFormEloquentUpdaterTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,29 @@ public function update($id, array $data)
175175
]);
176176
});
177177

178+
it('allows to have a field with the same name of a model method but not a relation', function () {
179+
$person = Person::create(['name' => 'Marie Curry']);
180+
181+
$form = new class() extends FakeSharpForm
182+
{
183+
use WithSharpFormEloquentUpdater;
184+
185+
public function buildFormFields(FieldsContainer $formFields): void
186+
{
187+
$formFields->addField(SharpFormTextField::make('unrelated'));
188+
}
189+
190+
public function update($id, array $data)
191+
{
192+
return $this->save(Person::findOrFail($id), $data);
193+
}
194+
};
195+
196+
$form->update($person->id, $form->formatAndValidateRequestData(['unrelated' => 'Marie Curie']));
197+
198+
expect($person->fresh()->unrelated)->toBe('Marie Curie');
199+
});
200+
178201
it('allows to update a belongsTo attribute', function () {
179202
$pierre = Person::create(['name' => 'Pierre Curie']);
180203
$marie = Person::create(['name' => 'Marie Curie']);

0 commit comments

Comments
 (0)