Skip to content

Commit

Permalink
added support for SoftDeleting in RelationJoiner
Browse files Browse the repository at this point in the history
  • Loading branch information
jarektkaczyk committed Mar 10, 2018
1 parent e7ff0e0 commit e941b7f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
5 changes: 5 additions & 0 deletions src/Relations/Joiner.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use LogicException;
use Illuminate\Database\Query\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Query\JoinClause as Join;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Illuminate\Database\Eloquent\Relations\Relation;
Expand Down Expand Up @@ -134,6 +135,10 @@ protected function getJoinClause(Model $parent, Relation $relation, $table, $typ

$join = (new Join($this->query, $type, $table))->on($fk, '=', $pk);

if (in_array(SoftDeletes::class, class_uses_recursive($relation->getRelated()))) {
$join->whereNull($relation->getRelated()->getQualifiedDeletedAtColumn());
}

if ($relation instanceof MorphOneOrMany) {
$join->where($relation->getQualifiedMorphType(), '=', $parent->getMorphClass());
}
Expand Down
15 changes: 12 additions & 3 deletions tests/JoinerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,13 @@ public function it_joins_relations_on_eloquent_builder()
public function it_joins_nested_relations_with_soft_delete()
{
$sql = 'select * from "users" '.
'inner join "posts" on "users"."id" = "posts"."user_id" and "posts"."deleted_at" is null '.
'inner join "comments" on "posts"."id" = "comments"."post_id" ';
'inner join "posts" on "posts"."user_id" = "users"."id" and "posts"."deleted_at" is null '.
'inner join "comments" on "comments"."post_id" = "posts"."id"';

$query = $this->getQuery();
$joiner = $this->factory->make($query);

$joiner->join('posts.comments');
$joiner->join('softDeletingPosts.comments');

$this->assertEquals($sql, $query->toSql());
}
Expand Down Expand Up @@ -148,6 +148,11 @@ public function posts()
return $this->hasMany('Sofa\Eloquence\Tests\JoinerPostStub', 'user_id');
}

public function softDeletingPosts()
{
return $this->hasMany('Sofa\Eloquence\Tests\JoinerSoftDeletingStub', 'user_id');
}

public function morphed()
{
return $this->morphOne('Sofa\Eloquence\Tests\MorphOneStub');
Expand All @@ -173,6 +178,10 @@ class JoinerCompanyStub extends Model {
}

class JoinerPostStub extends Model {
protected $table = 'posts';
}

class JoinerSoftDeletingStub extends Model {
use SoftDeletes;

protected $table = 'posts';
Expand Down

0 comments on commit e941b7f

Please sign in to comment.