Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion src/Form/Eloquent/EloquentModelUpdater.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions tests/Fixtures/Person.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,9 @@ public function upload(): MorphOne
{
return $this->morphOne(SharpUploadModel::class, 'model');
}

public function unrelated(): string
{
return 'unrelated';
}
}
1 change: 1 addition & 0 deletions tests/Pest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
$table->unsignedTinyInteger('order')->nullable();
$table->unsignedInteger('partner_id')->nullable();
$table->unsignedInteger('chief_id')->nullable();
$table->string('unrelated')->nullable();
$table->timestamps();
});

Expand Down
23 changes: 23 additions & 0 deletions tests/Unit/Form/Eloquent/SharpFormEloquentUpdaterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🍛


$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']);
Expand Down
Loading