Skip to content
Closed
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
15 changes: 7 additions & 8 deletions src/Illuminate/Database/Eloquent/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -2397,19 +2397,19 @@ public function getAttribute($key)
// If the key already exists in the relationships array, it just means the
// relationship has already been loaded, so we'll just return it out of
// here because there is no need to query within the relations twice.
if (array_key_exists($key, $this->relations))
$camelKey = camel_case($key);

if (array_key_exists($camelKey, $this->relations))
{
return $this->relations[$key];
return $this->relations[$camelKey];
}

// If the "attribute" exists as a method on the model, we will just assume
// it is a relationship and will load and return results from the query
// and hydrate the relationship's value on the "relationships" array.
$camelKey = camel_case($key);

if (method_exists($this, $camelKey))
{
return $this->getRelationshipFromMethod($key, $camelKey);
return $this->getRelationshipFromMethod($camelKey);
}
}

Expand Down Expand Up @@ -2460,14 +2460,13 @@ protected function getAttributeFromArray($key)
* Get a relationship value from a method.
*
* @param string $key
* @param string $camelKey
* @return mixed
*
* @throws \LogicException
*/
protected function getRelationshipFromMethod($key, $camelKey)
protected function getRelationshipFromMethod($key)
{
$relations = $this->$camelKey();
$relations = $this->$key();

if ( ! $relations instanceof Relation)
{
Expand Down
31 changes: 31 additions & 0 deletions tests/Database/DatabaseEloquentModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -842,6 +842,30 @@ public function testGetModelAttributeMethodThrowsExceptionIfNotRelation()
}


public function testGetRelationAttributeReturnsSameCollectionEachTime()
{
$model = new EloquentModelWithHasManyRelation;

$data = array(array('name' => 'Taylor'), array('name' => 'Otwell'));
$collection = EloquentModelStub::hydrate($data);

$model->setRelation('testRelation', $collection);
$this->assertSame($collection, $model->testRelation);
}


public function testGetRelationAttributeReturnsSameCollectionEachTimeWithCamelCase()
{
$model = new EloquentModelWithHasManyRelation;

$data = array(array('name' => 'Taylor'), array('name' => 'Otwell'));
$collection = EloquentModelStub::hydrate($data);

$model->setRelation('testRelation', $collection);
$this->assertSame($collection, $model->test_relation);
}


public function testModelIsBootedOnUnserialize()
{
$model = new EloquentModelBootingTestStub;
Expand Down Expand Up @@ -1117,3 +1141,10 @@ public function getIsAdminAttribute()
return 'admin';
}
}

class EloquentModelWithHasManyRelation extends Illuminate\Database\Eloquent\Model {
public function testRelation()
{
return $this->hasMany('EloquentModelStub');
}
}