Skip to content

Commit bcf0697

Browse files
authored
Merge pull request #191 from aygee/get-form-value-related-model
Getting form value from related model
2 parents d79805a + 0625f71 commit bcf0697

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

src/Eloquent/FormAccessible.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ public function getFormValue($key)
3939
// retrieval from the model to a form that is more useful for usage.
4040
if ($this->hasFormMutator($key)) {
4141
return $this->mutateFormAttribute($key, $value);
42+
} else {
43+
44+
// No form mutator, let the model resolve this
45+
return data_get($this, $key);
4246
}
4347

4448
return $value;

src/FormBuilder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1107,7 +1107,7 @@ public function getValueAttribute($name, $value = null)
11071107
protected function getModelValueAttribute($name)
11081108
{
11091109
if (method_exists($this->model, 'getFormValue')) {
1110-
return $this->model->getFormValue($name);
1110+
return $this->model->getFormValue($this->transformKey($name));
11111111
}
11121112

11131113
return data_get($this->model, $this->transformKey($name));

tests/FormAccessibleTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ public function setUp()
2424
$this->modelData = [
2525
'string' => 'abcdefghijklmnop',
2626
'email' => 'tj@tjshafer.com',
27+
'address' => [
28+
'street' => 'abcde st'
29+
],
2730
'created_at' => $this->now,
2831
'updated_at' => $this->now,
2932
];
@@ -43,6 +46,12 @@ public function testItCanMutateValuesForForms()
4346
$this->assertEquals($model->getFormValue('created_at'), $this->now->timestamp);
4447
}
4548

49+
public function testItCanGetRelatedValueForForms()
50+
{
51+
$model = new ModelThatUsesForms($this->modelData);
52+
$this->assertEquals($model->getFormValue('address.street'), 'abcde st');
53+
}
54+
4655
public function testItCanStillMutateValuesForViews()
4756
{
4857
$model = new ModelThatUsesForms($this->modelData);

0 commit comments

Comments
 (0)