From 50ff8f95fcf770eb6962168b6ff8ab0c33b9fe75 Mon Sep 17 00:00:00 2001 From: Jurian Arie <28654085+JurianArie@users.noreply.github.com> Date: Thu, 7 Oct 2021 15:29:26 +0200 Subject: [PATCH] [8.x] Revert "[8.x] Add nested relationships to whereRelation function (#39064)" (#39130) * Revert "[8.x] Add nested relationships to whereRelation function (#39064)" This reverts commit bc92a9614d35fd20df1ce5f1a3d28b90a646013a. * Add tests for nested whereRelation queries --- .../Database/EloquentWhereHasTest.php | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/tests/Integration/Database/EloquentWhereHasTest.php b/tests/Integration/Database/EloquentWhereHasTest.php index c775cc78a5ed..4d5d12e9b044 100644 --- a/tests/Integration/Database/EloquentWhereHasTest.php +++ b/tests/Integration/Database/EloquentWhereHasTest.php @@ -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'); @@ -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() @@ -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(); @@ -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;