Skip to content

Commit fcb8da8

Browse files
committed
Bugfix
1 parent 953b580 commit fcb8da8

File tree

5 files changed

+33
-19
lines changed

5 files changed

+33
-19
lines changed

CHANGELOG.md

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,14 @@
22

33
All notable changes to `laravel-eloquent-scope-as-select` will be documented in this file
44

5-
## 1.4.0 - 2020-10-28
5+
## 1.1.1 - 2020-12-14
66

7-
- Allow empty search terms
8-
- Added `new()` method method
7+
- Bugfix for counting relations from the same model
98

10-
## 1.3.1 - 2020-10-28
9+
## 1.1.0 - 2020-12-08
1110

12-
- Docs
11+
- Call scopes with a string or array with additional constraints
1312

14-
## 1.3.0 - 2020-09-24
15-
16-
- Support for Laravel 8.0
17-
18-
## 1.2.0 - 2020-08-28
19-
20-
- standalone search terms parser
21-
22-
## 1.1.0 - 2020-08-10
23-
24-
- option to disable the parsing of the search term
25-
26-
## 1.0.0 - 2020-07-08
13+
## 1.0.0 - 2020-12-03
2714

2815
- initial release

src/ScopeAsSelect.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public static function addMacro(string $name = 'addScopeAsSelect')
7373

7474
// Query the model and explicitly set the targetted table, as the model's table
7575
// is just the aliased table with the 'as' statement.
76-
$subSelect = $aliasedModel::query();
76+
$subSelect = $aliasedModel::query()->setModel($aliasedModel);
7777
$subSelect->getQuery()->from($originalTable, $aliasedTable);
7878

7979
// Apply the where constraint based on the model's key name and apply the $callable.

tests/Comment.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,8 @@
66

77
class Comment extends Model
88
{
9+
public function comments()
10+
{
11+
return $this->hasMany(Comment::class, 'parent_comment_id');
12+
}
913
}

tests/ScopeAsSelectTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,4 +191,26 @@ public function it_can_mix_scopes_outside_of_the_closure()
191191
$this->assertFalse($posts->get(0)->title_is_foo_and_has_six_comments_or_more);
192192
$this->assertTrue($posts->get(1)->title_is_foo_and_has_six_comments_or_more);
193193
}
194+
195+
/** @test */
196+
public function it_can_check_for_children_of_the_same_model()
197+
{
198+
$post = Post::create(['title' => 'bar']);
199+
200+
$commentParent = $post->comments()->create(['body' => 'ok']);
201+
$commentChild = $post->comments()->create(['body' => 'ok', 'parent_comment_id' => $commentParent->getKey()]);
202+
203+
$comments = Comment::query()
204+
->addScopeAsSelect('has_children', function ($query) {
205+
$query->has('comments');
206+
})
207+
->orderBy('id')
208+
->get();
209+
210+
$this->assertCount(2, $comments);
211+
$this->assertTrue($comments->contains($commentParent));
212+
$this->assertTrue($comments->contains($commentChild));
213+
$this->assertTrue($comments->get(0)->has_children);
214+
$this->assertFalse($comments->get(1)->has_children);
215+
}
194216
}

tests/create_tables.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public function up()
2525
$table->unsignedInteger('post_id');
2626
$table->string('body');
2727
$table->date('published_at')->nullable();
28+
$table->unsignedInteger('parent_comment_id')->nullable();
2829
$table->timestamps();
2930
});
3031

0 commit comments

Comments
 (0)