Skip to content

Commit 86fc0ca

Browse files
committed
Merge pull request #1976 from neoascetic/more_fluent_eloquent
Even more fluent eloquent model via magic setters
2 parents 727b694 + f2f1d4d commit 86fc0ca

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

laravel/database/eloquent/model.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ public function timestamp()
441441
}
442442

443443
/**
444-
*Updates the timestamp on the model and immediately saves it.
444+
* Updates the timestamp on the model and immediately saves it.
445445
*
446446
* @return void
447447
*/
@@ -562,11 +562,12 @@ public function get_attribute($key)
562562
*
563563
* @param string $key
564564
* @param mixed $value
565-
* @return void
565+
* @return Model
566566
*/
567567
public function set_attribute($key, $value)
568568
{
569569
$this->attributes[$key] = $value;
570+
return $this;
570571
}
571572

572573
/**
@@ -769,7 +770,7 @@ public function __call($method, $parameters)
769770
}
770771
elseif (starts_with($method, 'set_'))
771772
{
772-
$this->set_attribute(substr($method, 4), $parameters[0]);
773+
return $this->set_attribute(substr($method, 4), $parameters[0]);
773774
}
774775

775776
// Finally we will assume that the method is actually the beginning of a

laravel/tests/cases/eloquent.test.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,19 @@ public function testAttributeMagicSetterMethodChangesAttribute()
133133
Model::$accessible = null;
134134
}
135135

136+
/**
137+
* Test the Model::__set method allows chaining.
138+
*
139+
* @group laravel
140+
*/
141+
public function testAttributeMagicSetterMethodAllowsChaining()
142+
{
143+
$model = new Model;
144+
$this->assertInstanceOf('Model', $model->set_foo('foo'));
145+
$model->set_bar('bar')->set_baz('baz');
146+
$this->assertEquals(array('foo' => 'foo', 'bar' => 'bar', 'baz' => 'baz'), $model->to_array());
147+
}
148+
136149
/**
137150
* Test the Model::__get method.
138151
*
@@ -288,4 +301,4 @@ public function testConvertingToArray()
288301

289302
}
290303

291-
}
304+
}

0 commit comments

Comments
 (0)