Skip to content

Commit

Permalink
Add without() method to remove specified eager loads
Browse files Browse the repository at this point in the history
  • Loading branch information
JayBizzle committed Jun 17, 2016
1 parent 01c9e08 commit 1d52300
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/Illuminate/Database/Eloquent/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -1031,6 +1031,23 @@ public function with($relations)
return $this;
}

/**
* Remove any relationships that have been set to be eager loaded.
*
* @param mixed $relations
* @return $this
*/
public function without($relations)
{
if (is_string($relations)) {
$relations = func_get_args();
}

$this->eagerLoad = array_diff_key($this->eagerLoad, array_flip($relations));

return $this;
}

/**
* Add subselect queries to count the relations.
*
Expand Down
19 changes: 19 additions & 0 deletions tests/Database/DatabaseEloquentModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,13 @@ public function testWithMethodCallsQueryBuilderCorrectly()
$this->assertEquals('foo', $result);
}

public function testWithoutMethodRemovesEagerLoadedRelationshipCorrectly()
{
$model = new EloquentModelWithoutRelationStub;
$instance = $model->newInstance()->newQuery()->without('foo');
$this->assertEmpty($instance->getEagerLoads());
}

public function testWithMethodCallsQueryBuilderCorrectlyWithArray()
{
$result = EloquentModelWithStub::with(['foo', 'bar']);
Expand Down Expand Up @@ -1483,6 +1490,18 @@ public function newQuery()
}
}

class EloquentModelWithoutRelationStub extends Model
{
public $with = ['foo'];

protected $guarded = [];

public function getEagerLoads()
{
return $this->eagerLoads;
}
}

class EloquentModelWithoutTableStub extends Model
{
}
Expand Down

0 comments on commit 1d52300

Please sign in to comment.