Skip to content

Commit

Permalink
Bugfix
Browse files Browse the repository at this point in the history
  • Loading branch information
pascalbaljet committed Dec 14, 2020
1 parent 953b580 commit fcb8da8
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 19 deletions.
23 changes: 5 additions & 18 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion src/ScopeAsSelect.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 4 additions & 0 deletions tests/Comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@

class Comment extends Model
{
public function comments()
{
return $this->hasMany(Comment::class, 'parent_comment_id');
}
}
22 changes: 22 additions & 0 deletions tests/ScopeAsSelectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
1 change: 1 addition & 0 deletions tests/create_tables.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});

Expand Down

0 comments on commit fcb8da8

Please sign in to comment.