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
laravel#39064)" (laravel#39130)

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

This reverts commit bc92a96.

* Add tests for nested whereRelation queries
  • Loading branch information
JurianArie authored and chu121su12 committed Oct 7, 2021
1 parent 4f09e02 commit 50ff8f9
Showing 1 changed file with 39 additions and 0 deletions.
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()
$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()
$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 50ff8f9

Please sign in to comment.