Skip to content

Commit

Permalink
Getting relation data for model accessor (LaravelCollective#474)
Browse files Browse the repository at this point in the history
  • Loading branch information
Vendin authored and tshafer committed Feb 5, 2018
1 parent 32c27a5 commit 137a2ef
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Eloquent/FormAccessible.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ public function getFormValue($key)
unset($keys[0]);
$key = implode('.', $keys);

if (method_exists($relatedModel, 'hasFormMutator') && $relatedModel->hasFormMutator($key)) {
if (method_exists($relatedModel, 'hasFormMutator') && $key !== '' && $relatedModel->hasFormMutator($key)) {
return $relatedModel->getFormValue($key);
}

return data_get($relatedModel, $key);
return data_get($relatedModel, empty($key)? null: $key);
}

// No form mutator, let the model resolve this
Expand Down
8 changes: 8 additions & 0 deletions tests/FormAccessibleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ public function testItCanMutateValuesForForms()

$this->assertEquals($model->getFormValue('string'), 'ponmlkjihgfedcba');
$this->assertEquals($model->getFormValue('created_at'), $this->now->timestamp);

$this->assertEquals($user, $model->getFormValue('user'));
$this->assertEquals('Get name: Anton', $model->getFormValue('user')->name);
$this->assertEquals('Get name for form: Anton', $model->getFormValue('user.name'));
}

Expand Down Expand Up @@ -171,4 +174,9 @@ public function formNameAttribute($value)
{
return 'Get name for form: ' . $value;
}

public function getNameAttribute($value)
{
return 'Get name: ' . $value;
}
}

0 comments on commit 137a2ef

Please sign in to comment.