Skip to content

Commit 34a164f

Browse files
committed
Merge pull request laravel#8246 from jarektkaczyk/5.0-parent-softDeletes-on-hasManyThrough
[5.0] Ignore soft deleted close parents on HasManyThrough
2 parents 69ff873 + 268760d commit 34a164f

2 files changed

Lines changed: 32 additions & 0 deletions

File tree

src/Illuminate/Database/Eloquent/Relations/HasManyThrough.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,21 @@ protected function setJoin(Builder $query = null)
9797
$foreignKey = $this->related->getTable().'.'.$this->secondKey;
9898

9999
$query->join($this->parent->getTable(), $this->getQualifiedParentKeyName(), '=', $foreignKey);
100+
101+
if ($this->parentSoftDeletes())
102+
{
103+
$query->whereNull($this->parent->getQualifiedDeletedAtColumn());
104+
}
105+
}
106+
107+
/**
108+
* Determine whether close parent of the relation uses Soft Deletes.
109+
*
110+
* @return bool
111+
*/
112+
public function parentSoftDeletes()
113+
{
114+
return in_array('Illuminate\Database\Eloquent\SoftDeletes', class_uses_recursive(get_class($this->parent)));
100115
}
101116

102117
/**

tests/Database/DatabaseEloquentHasManyThroughTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use Mockery as m;
44
use Illuminate\Database\Eloquent\Collection;
55
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
6+
use Illuminate\Database\Eloquent\SoftDeletes;
67

78
class DatabaseEloquentHasManyThroughTest extends PHPUnit_Framework_TestCase {
89

@@ -139,6 +140,17 @@ public function testFindManyMethod()
139140
}
140141

141142

143+
public function testIgnoreSoftDeletingParent()
144+
{
145+
list($builder, $country,, $firstKey, $secondKey) = $this->getRelationArguments();
146+
$user = new EloquentHasManyThroughSoftDeletingModelStub;
147+
148+
$builder->shouldReceive('whereNull')->with('users.deleted_at')->once()->andReturn($builder);
149+
150+
$relation = new HasManyThrough($builder, $country, $user, $firstKey, $secondKey);
151+
}
152+
153+
142154
protected function getRelation()
143155
{
144156
list($builder, $country, $user, $firstKey, $secondKey) = $this->getRelationArguments();
@@ -176,3 +188,8 @@ protected function getRelationArguments()
176188
class EloquentHasManyThroughModelStub extends Illuminate\Database\Eloquent\Model {
177189
public $country_id = 'foreign.value';
178190
}
191+
192+
class EloquentHasManyThroughSoftDeletingModelStub extends Illuminate\Database\Eloquent\Model {
193+
use SoftDeletes;
194+
public $table = 'users';
195+
}

0 commit comments

Comments
 (0)