Skip to content

Commit

Permalink
[8.x] Revert "[8.x] Add nested relationships to whereRelation function (
Browse files Browse the repository at this point in the history
#39064)" (#39130)

* Revert "[8.x] Add nested relationships to whereRelation function (#39064)"

This reverts commit bc92a96.

* Add tests for nested whereRelation queries
  • Loading branch information
JurianArie authored Oct 7, 2021
1 parent 51e9106 commit 1557042
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 34 deletions.
40 changes: 6 additions & 34 deletions src/Illuminate/Database/Eloquent/Concerns/QueriesRelationships.php
Original file line number Diff line number Diff line change
Expand Up @@ -362,23 +362,9 @@ public function orWhereDoesntHaveMorph($relation, $types, Closure $callback = nu
*/
public function whereRelation($relation, $column, $operator = null, $value = null)
{
$relations = collect(explode('.', $relation));

return $this->when(
$relations->count() == 1,
function ($query) use ($relations, $column, $operator, $value) {
$query->whereHas($relations->first(), function ($query) use ($column, $operator, $value) {
$query->where($column, $operator, $value);
});
},
function ($query) use ($relations, $column, $operator, $value) {
$query->whereHas($relations->first(), function ($query) use ($relations, $column, $operator, $value) {
$relations->shift();

$query->whereRelation($relations->implode('.'), $column, $operator, $value);
});
}
);
return $this->whereHas($relation, function ($query) use ($column, $operator, $value) {
$query->where($column, $operator, $value);
});
}

/**
Expand All @@ -392,23 +378,9 @@ function ($query) use ($relations, $column, $operator, $value) {
*/
public function orWhereRelation($relation, $column, $operator = null, $value = null)
{
$relations = collect(explode('.', $relation));

return $this->when(
$relations->count() == 1,
function ($query) use ($relations, $column, $operator, $value) {
$query->orWhereHas($relations->first(), function ($query) use ($column, $operator, $value) {
$query->where($column, $operator, $value);
});
},
function ($query) use ($relations, $column, $operator, $value) {
$query->orWhereHas($relations->first(), function ($query) use ($relations, $column, $operator, $value) {
$relations->shift();

$query->orWhereRelation($relations->implode('.'), $column, $operator, $value);
});
}
);
return $this->orWhereHas($relation, function ($query) use ($column, $operator, $value) {
$query->where($column, $operator, $value);
});
}

/**
Expand Down
39 changes: 39 additions & 0 deletions tests/Integration/Database/EloquentWhereHasTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ protected function setUp(): void
$table->boolean('public');
});

Schema::create('texts', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('post_id');
$table->boolean('content');
});

Schema::create('comments', function (Blueprint $table) {
$table->increments('id');
$table->string('commentable_type');
Expand All @@ -35,10 +41,12 @@ protected function setUp(): void
$user = User::create();
$post = tap((new Post(['public' => true]))->user()->associate($user))->save();
(new Comment)->commentable()->associate($post)->save();
(new Text(['content' => 'test']))->post()->associate($post)->save();

$user = User::create();
$post = tap((new Post(['public' => false]))->user()->associate($user))->save();
(new Comment)->commentable()->associate($post)->save();
(new Text(['content' => 'test2']))->post()->associate($post)->save();
}

public function testWhereRelation()
Expand All @@ -55,6 +63,20 @@ public function testOrWhereRelation()
$this->assertEquals([1, 2], $users->pluck('id')->all());
}

public function testNestedWhereRelation()
{
$texts = User::whereRelation('posts.texts', 'content', 'test')->get();

$this->assertEquals([1], $texts->pluck('id')->all());
}

public function testNestedOrWhereRelation()
{
$texts = User::whereRelation('posts.texts', 'content', 'test')->orWhereRelation('posts.texts', 'content', 'test2')->get();

$this->assertEquals([1, 2], $texts->pluck('id')->all());
}

public function testWhereMorphRelation()
{
$comments = Comment::whereMorphRelation('commentable', '*', 'public', true)->get();
Expand Down Expand Up @@ -104,12 +126,29 @@ public function comments()
return $this->morphMany(Comment::class, 'commentable');
}

public function texts()
{
return $this->hasMany(Text::class);
}

public function user()
{
return $this->belongsTo(User::class);
}
}

class Text extends Model
{
public $timestamps = false;

protected $guarded = [];

public function post()
{
return $this->belongsTo(Post::class);
}
}

class User extends Model
{
public $timestamps = false;
Expand Down

0 comments on commit 1557042

Please sign in to comment.