diff --git a/src/Form/Eloquent/EloquentModelUpdater.php b/src/Form/Eloquent/EloquentModelUpdater.php index 1df254a0f..f11c7a606 100644 --- a/src/Form/Eloquent/EloquentModelUpdater.php +++ b/src/Form/Eloquent/EloquentModelUpdater.php @@ -5,7 +5,10 @@ use Closure; use Illuminate\Contracts\Support\Arrayable; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\Relations\Relation; use ReflectionClass; +use ReflectionMethod; +use ReflectionNamedType; class EloquentModelUpdater { @@ -73,7 +76,18 @@ protected function valuateAttribute(Model $instance, string $attribute, $value): protected function isRelationship(Model $instance, string $attribute): bool { - return str($attribute)->contains(':') || $instance->isRelation($attribute); + if(str($attribute)->contains(':')) { + return true; + } + + if($instance->isRelation($attribute)) { + $returnType = (new ReflectionMethod($instance, $attribute))->getReturnType(); + + return $returnType instanceof ReflectionNamedType + && is_subclass_of($returnType->getName(), Relation::class); + } + + return false; } protected function saveRelationships(Model $instance): void diff --git a/tests/Fixtures/Person.php b/tests/Fixtures/Person.php index 0b4d70940..f78f6beb3 100644 --- a/tests/Fixtures/Person.php +++ b/tests/Fixtures/Person.php @@ -55,4 +55,9 @@ public function upload(): MorphOne { return $this->morphOne(SharpUploadModel::class, 'model'); } + + public function unrelated(): string + { + return 'unrelated'; + } } diff --git a/tests/Pest.php b/tests/Pest.php index 5bbde429f..76ceca638 100644 --- a/tests/Pest.php +++ b/tests/Pest.php @@ -20,6 +20,7 @@ $table->unsignedTinyInteger('order')->nullable(); $table->unsignedInteger('partner_id')->nullable(); $table->unsignedInteger('chief_id')->nullable(); + $table->string('unrelated')->nullable(); $table->timestamps(); }); diff --git a/tests/Unit/Form/Eloquent/SharpFormEloquentUpdaterTest.php b/tests/Unit/Form/Eloquent/SharpFormEloquentUpdaterTest.php index 475c67718..01c7fdf32 100644 --- a/tests/Unit/Form/Eloquent/SharpFormEloquentUpdaterTest.php +++ b/tests/Unit/Form/Eloquent/SharpFormEloquentUpdaterTest.php @@ -175,6 +175,29 @@ public function update($id, array $data) ]); }); +it('allows to have a field with the same name of a model method but not a relation', function () { + $person = Person::create(['name' => 'Marie Curry']); + + $form = new class() extends FakeSharpForm + { + use WithSharpFormEloquentUpdater; + + public function buildFormFields(FieldsContainer $formFields): void + { + $formFields->addField(SharpFormTextField::make('unrelated')); + } + + public function update($id, array $data) + { + return $this->save(Person::findOrFail($id), $data); + } + }; + + $form->update($person->id, $form->formatAndValidateRequestData(['unrelated' => 'Marie Curie'])); + + expect($person->fresh()->unrelated)->toBe('Marie Curie'); +}); + it('allows to update a belongsTo attribute', function () { $pierre = Person::create(['name' => 'Pierre Curie']); $marie = Person::create(['name' => 'Marie Curie']);