@@ -19,6 +19,13 @@ protected function afterRefreshingDatabase()
19
19
$ table ->timestamps ();
20
20
});
21
21
22
+ Schema::create ('test_post_with_global_scopes ' , function (Blueprint $ table ) {
23
+ $ table ->increments ('id ' );
24
+ $ table ->string ('title ' )->nullable ();
25
+ $ table ->unsignedInteger ('user_id ' )->nullable ();
26
+ $ table ->timestamps ();
27
+ });
28
+
22
29
Schema::create ('test_users ' , function ($ table ) {
23
30
$ table ->increments ('id ' );
24
31
$ table ->string ('name ' )->nullable ();
@@ -54,6 +61,50 @@ public function testPaginationWithUnion()
54
61
$ this ->assertSame (['user_id ' ], $ result ->getOptions ()['parameters ' ]);
55
62
}
56
63
64
+ public function testPaginationWithUnionAndGlobalScopes ()
65
+ {
66
+ TestPostWithGlobalScope::create (['title ' => 'Hello world ' , 'user_id ' => 1 ]);
67
+ TestPostWithGlobalScope::create (['title ' => 'Goodbye world ' , 'user_id ' => 2 ]);
68
+ TestPostWithGlobalScope::create (['title ' => 'Howdy ' , 'user_id ' => 3 ]);
69
+ TestPostWithGlobalScope::create (['title ' => '4th ' , 'user_id ' => 4 ]);
70
+
71
+ $ table1 = TestPostWithGlobalScope::select ('id ' )->whereIn ('user_id ' , [1 , 2 ]);
72
+ $ table2 = TestPostWithGlobalScope::select ('id ' )->whereIn ('user_id ' , [3 , 4 ]);
73
+
74
+ $ columns = ['id ' ];
75
+ $ cursorName = 'cursor-name ' ;
76
+ $ cursor = new Cursor (['id ' => 1 ]);
77
+
78
+ $ result = $ table1
79
+ ->union ($ table2 )
80
+ ->orderBy ('id ' , 'asc ' )
81
+ ->cursorPaginate (1 , $ columns , $ cursorName , $ cursor );
82
+
83
+ $ this ->assertSame (['id ' ], $ result ->getOptions ()['parameters ' ]);
84
+ }
85
+
86
+ public function testPaginationWithUnionAndGlobalScopesToBase ()
87
+ {
88
+ TestPostWithGlobalScope::create (['title ' => 'Hello world ' , 'user_id ' => 1 ]);
89
+ TestPostWithGlobalScope::create (['title ' => 'Goodbye world ' , 'user_id ' => 2 ]);
90
+ TestPostWithGlobalScope::create (['title ' => 'Howdy ' , 'user_id ' => 3 ]);
91
+ TestPostWithGlobalScope::create (['title ' => '4th ' , 'user_id ' => 4 ]);
92
+
93
+ $ table1 = TestPostWithGlobalScope::select ('id ' )->whereIn ('user_id ' , [1 , 2 ]);
94
+ $ table2 = TestPostWithGlobalScope::select ('id ' )->whereIn ('user_id ' , [3 , 4 ]);
95
+
96
+ $ columns = ['id ' ];
97
+ $ cursorName = 'cursor-name ' ;
98
+ $ cursor = new Cursor (['id ' => 1 ]);
99
+
100
+ $ result = $ table1
101
+ ->union ($ table2 ->toBase ())
102
+ ->orderBy ('id ' , 'asc ' )
103
+ ->cursorPaginate (1 , $ columns , $ cursorName , $ cursor );
104
+
105
+ $ this ->assertSame (['id ' ], $ result ->getOptions ()['parameters ' ]);
106
+ }
107
+
57
108
public function testPaginationWithDistinct ()
58
109
{
59
110
for ($ i = 1 ; $ i <= 3 ; $ i ++) {
@@ -181,9 +232,9 @@ public function testPaginationWithMultipleUnionAndMultipleWhereClauses()
181
232
$ cursorName = 'cursor-name ' ;
182
233
$ cursor = new Cursor (['id ' => 1 ]);
183
234
184
- $ result = $ table1-> toBase ()
185
- ->union ($ table2-> toBase () )
186
- ->union ($ table3-> toBase () )
235
+ $ result = $ table1
236
+ ->union ($ table2 )
237
+ ->union ($ table3 )
187
238
->orderBy ('id ' , 'asc ' )
188
239
->cursorPaginate (1 , $ columns , $ cursorName , $ cursor );
189
240
@@ -284,6 +335,20 @@ class TestPost extends Model
284
335
protected $ guarded = [];
285
336
}
286
337
338
+ class TestPostWithGlobalScope extends Model
339
+ {
340
+ protected $ guarded = [];
341
+
342
+ public static function boot ()
343
+ {
344
+ parent ::boot ();
345
+
346
+ static ::addGlobalScope ('global ' , function ($ builder ) {
347
+ $ builder ->where ('id ' , '> ' , 0 );
348
+ });
349
+ }
350
+ }
351
+
287
352
class TestUser extends Model
288
353
{
289
354
protected $ guarded = [];
0 commit comments