diff --git a/CHANGELOG.md b/CHANGELOG.md index 49eebd0..a736630 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,27 +2,14 @@ All notable changes to `laravel-eloquent-scope-as-select` will be documented in this file -## 1.4.0 - 2020-10-28 +## 1.1.1 - 2020-12-14 -- Allow empty search terms -- Added `new()` method method +- Bugfix for counting relations from the same model -## 1.3.1 - 2020-10-28 +## 1.1.0 - 2020-12-08 -- Docs +- Call scopes with a string or array with additional constraints -## 1.3.0 - 2020-09-24 - -- Support for Laravel 8.0 - -## 1.2.0 - 2020-08-28 - -- standalone search terms parser - -## 1.1.0 - 2020-08-10 - -- option to disable the parsing of the search term - -## 1.0.0 - 2020-07-08 +## 1.0.0 - 2020-12-03 - initial release diff --git a/src/ScopeAsSelect.php b/src/ScopeAsSelect.php index f22ddc2..273604e 100644 --- a/src/ScopeAsSelect.php +++ b/src/ScopeAsSelect.php @@ -73,7 +73,7 @@ public static function addMacro(string $name = 'addScopeAsSelect') // Query the model and explicitly set the targetted table, as the model's table // is just the aliased table with the 'as' statement. - $subSelect = $aliasedModel::query(); + $subSelect = $aliasedModel::query()->setModel($aliasedModel); $subSelect->getQuery()->from($originalTable, $aliasedTable); // Apply the where constraint based on the model's key name and apply the $callable. diff --git a/tests/Comment.php b/tests/Comment.php index 56dc2e2..1a9b206 100644 --- a/tests/Comment.php +++ b/tests/Comment.php @@ -6,4 +6,8 @@ class Comment extends Model { + public function comments() + { + return $this->hasMany(Comment::class, 'parent_comment_id'); + } } diff --git a/tests/ScopeAsSelectTest.php b/tests/ScopeAsSelectTest.php index 53c4a1d..dde2a3e 100644 --- a/tests/ScopeAsSelectTest.php +++ b/tests/ScopeAsSelectTest.php @@ -191,4 +191,26 @@ public function it_can_mix_scopes_outside_of_the_closure() $this->assertFalse($posts->get(0)->title_is_foo_and_has_six_comments_or_more); $this->assertTrue($posts->get(1)->title_is_foo_and_has_six_comments_or_more); } + + /** @test */ + public function it_can_check_for_children_of_the_same_model() + { + $post = Post::create(['title' => 'bar']); + + $commentParent = $post->comments()->create(['body' => 'ok']); + $commentChild = $post->comments()->create(['body' => 'ok', 'parent_comment_id' => $commentParent->getKey()]); + + $comments = Comment::query() + ->addScopeAsSelect('has_children', function ($query) { + $query->has('comments'); + }) + ->orderBy('id') + ->get(); + + $this->assertCount(2, $comments); + $this->assertTrue($comments->contains($commentParent)); + $this->assertTrue($comments->contains($commentChild)); + $this->assertTrue($comments->get(0)->has_children); + $this->assertFalse($comments->get(1)->has_children); + } } diff --git a/tests/create_tables.php b/tests/create_tables.php index a7c0682..42fd2ea 100644 --- a/tests/create_tables.php +++ b/tests/create_tables.php @@ -25,6 +25,7 @@ public function up() $table->unsignedInteger('post_id'); $table->string('body'); $table->date('published_at')->nullable(); + $table->unsignedInteger('parent_comment_id')->nullable(); $table->timestamps(); });