Permalink
Browse files

Merge pull request #2816 from Anahkiasen/contains-model

Allow Eloquent\Collection::find and contains to work with models
  • Loading branch information...
taylorotwell committed Nov 27, 2013
2 parents ea9a924 + e8dbfa7 commit 362db89e635eda87faf8c0db0935e3fe0d74f5bf
@@ -13,6 +13,11 @@ class Collection extends BaseCollection {
*/
public function find($key, $default = null)
{
if ($key instanceof Model)
{
$key = $key->getKey();
}
return array_first($this->items, function($itemKey, $model) use ($key)
{
return $model->getKey() == $key;
@@ -33,6 +33,22 @@ public function testGettingMinItemsFromCollection()
}
public function testContainsIndicatesIfModelInArray()
{
$mockModel = m::mock('Illuminate\Database\Eloquent\Model');
$mockModel->shouldReceive('getKey')->andReturn(1);
$mockModel2 = m::mock('Illuminate\Database\Eloquent\Model');
$mockModel2->shouldReceive('getKey')->andReturn(2);
$mockModel3 = m::mock('Illuminate\Database\Eloquent\Model');
$mockModel3->shouldReceive('getKey')->andReturn(3);
$c = new Collection(array($mockModel, $mockModel2));
$this->assertTrue($c->contains($mockModel));
$this->assertTrue($c->contains($mockModel2));
$this->assertFalse($c->contains($mockModel3));
}
public function testContainsIndicatesIfKeyedModelInArray()
{
$mockModel = m::mock('Illuminate\Database\Eloquent\Model');

0 comments on commit 362db89

Please sign in to comment.