File tree Expand file tree Collapse file tree 5 files changed +33
-19
lines changed Expand file tree Collapse file tree 5 files changed +33
-19
lines changed Original file line number Diff line number Diff line change 2
2
3
3
All notable changes to ` laravel-eloquent-scope-as-select ` will be documented in this file
4
4
5
- ## 1.4.0 - 2020-10-28
5
+ ## 1.1.1 - 2020-12-14
6
6
7
- - Allow empty search terms
8
- - Added ` new() ` method method
7
+ - Bugfix for counting relations from the same model
9
8
10
- ## 1.3.1 - 2020-10-28
9
+ ## 1.1.0 - 2020-12-08
11
10
12
- - Docs
11
+ - Call scopes with a string or array with additional constraints
13
12
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
27
14
28
15
- initial release
Original file line number Diff line number Diff line change @@ -73,7 +73,7 @@ public static function addMacro(string $name = 'addScopeAsSelect')
73
73
74
74
// Query the model and explicitly set the targetted table, as the model's table
75
75
// is just the aliased table with the 'as' statement.
76
- $ subSelect = $ aliasedModel ::query ();
76
+ $ subSelect = $ aliasedModel ::query ()-> setModel ( $ aliasedModel ) ;
77
77
$ subSelect ->getQuery ()->from ($ originalTable , $ aliasedTable );
78
78
79
79
// Apply the where constraint based on the model's key name and apply the $callable.
Original file line number Diff line number Diff line change 6
6
7
7
class Comment extends Model
8
8
{
9
+ public function comments ()
10
+ {
11
+ return $ this ->hasMany (Comment::class, 'parent_comment_id ' );
12
+ }
9
13
}
Original file line number Diff line number Diff line change @@ -191,4 +191,26 @@ public function it_can_mix_scopes_outside_of_the_closure()
191
191
$ this ->assertFalse ($ posts ->get (0 )->title_is_foo_and_has_six_comments_or_more );
192
192
$ this ->assertTrue ($ posts ->get (1 )->title_is_foo_and_has_six_comments_or_more );
193
193
}
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
+ }
194
216
}
Original file line number Diff line number Diff line change @@ -25,6 +25,7 @@ public function up()
25
25
$ table ->unsignedInteger ('post_id ' );
26
26
$ table ->string ('body ' );
27
27
$ table ->date ('published_at ' )->nullable ();
28
+ $ table ->unsignedInteger ('parent_comment_id ' )->nullable ();
28
29
$ table ->timestamps ();
29
30
});
30
31
You can’t perform that action at this time.
0 commit comments