7
7
use Illuminate \Database \Eloquent \{Builder as EloquentBuilder , Model };
8
8
use Illuminate \Database \Query \Builder as QueryBuilder ;
9
9
use Illuminate \Pagination \Paginator ;
10
- use Illuminate \Support \{Collection as BaseCollection , Facades \DB , Str };
10
+ use Illuminate \Support \{Collection as BaseCollection , Facades \DB , Str , Stringable };
11
+ use Laravel \Scout \Builder as ScoutBuilder ;
11
12
use PowerComponents \LivewirePowerGrid \Components \Actions \ActionsController ;
12
13
use PowerComponents \LivewirePowerGrid \Components \Rules \{RulesController };
13
14
use PowerComponents \LivewirePowerGrid \DataSource \{Builder , Collection };
@@ -18,8 +19,6 @@ class ProcessDataSource
18
19
{
19
20
use Concerns \SoftDeletes;
20
21
21
- public bool $ isCollection = false ;
22
-
23
22
private array $ queryLog = [];
24
23
25
24
public function __construct (
@@ -49,16 +48,46 @@ public function get(bool $isExport = false): Paginator|LengthAwarePaginator|\Ill
49
48
return $ this ->processCollection ($ datasource , $ isExport );
50
49
}
51
50
51
+ if ($ datasource instanceof ScoutBuilder) {
52
+ return $ this ->processScoutCollection ($ datasource );
53
+ }
54
+
52
55
$ this ->setCurrentTable ($ datasource );
53
56
54
- /** @phpstan-ignore-next-line */
55
- return $ this ->processModel ($ datasource );
57
+ return $ this ->processModel ($ datasource ); // @phpstan-ignore-line
56
58
}
57
59
58
- /**
59
- * @return EloquentBuilder|BaseCollection|Collection|QueryBuilder|MorphToMany|null
60
- */
61
- public function prepareDataSource (): EloquentBuilder |BaseCollection |Collection |QueryBuilder |MorphToMany |null
60
+ public function processScoutCollection (ScoutBuilder $ datasource ): Paginator |LengthAwarePaginator
61
+ {
62
+ $ datasource ->query = Str::of ($ datasource ->query )
63
+ ->when ($ this ->component ->search != '' , fn (Stringable $ self ) => $ self
64
+ ->prepend ($ this ->component ->search . ', ' ))
65
+ ->toString ();
66
+
67
+ collect ($ this ->component ->filters )->each (fn (array $ filters ) => collect ($ filters )
68
+ ->each (fn (string $ value , string $ field ) => $ datasource
69
+ ->where ($ field , $ value )));
70
+
71
+ if ($ this ->component ->multiSort ) {
72
+ foreach ($ this ->component ->sortArray as $ sortField => $ direction ) {
73
+ $ datasource ->orderBy ($ sortField , $ direction );
74
+ }
75
+ } else {
76
+ $ datasource ->orderBy ($ this ->component ->sortField , $ this ->component ->sortDirection );
77
+ }
78
+
79
+ $ results = self ::applyPerPage ($ datasource );
80
+
81
+ if (method_exists ($ results , 'total ' )) {
82
+ $ this ->component ->total = $ results ->total ();
83
+ }
84
+
85
+ return $ results ->setCollection ( // @phpstan-ignore-line
86
+ $ this ->transform ($ results ->getCollection (), $ this ->component ) // @phpstan-ignore-line
87
+ );
88
+ }
89
+
90
+ public function prepareDataSource (): EloquentBuilder |BaseCollection |Collection |QueryBuilder |MorphToMany |ScoutBuilder |null
62
91
{
63
92
$ datasource = $ this ->component ->datasource ?? null ;
64
93
@@ -70,8 +99,6 @@ public function prepareDataSource(): EloquentBuilder|BaseCollection|Collection|Q
70
99
$ datasource = collect ($ datasource );
71
100
}
72
101
73
- $ this ->isCollection = $ datasource instanceof BaseCollection;
74
-
75
102
return $ datasource ;
76
103
}
77
104
@@ -226,7 +253,7 @@ private function applyWithSortStringNumber(
226
253
return $ results ;
227
254
}
228
255
229
- private function applyPerPage (EloquentBuilder |QueryBuilder |MorphToMany $ results ): LengthAwarePaginator |Paginator
256
+ private function applyPerPage (EloquentBuilder |QueryBuilder |MorphToMany | ScoutBuilder $ results ): LengthAwarePaginator |Paginator
230
257
{
231
258
$ pageName = strval (data_get ($ this ->component ->setUp , 'footer.pageName ' , 'page ' ));
232
259
$ perPage = intval (data_get ($ this ->component ->setUp , 'footer.perPage ' ));
@@ -237,6 +264,10 @@ private function applyPerPage(EloquentBuilder|QueryBuilder|MorphToMany $results)
237
264
default => 'paginate ' ,
238
265
};
239
266
267
+ if ($ results instanceof ScoutBuilder) {
268
+ return $ results ->paginateSafe ($ perPage , pageName: $ pageName ); // @phpstan-ignore-line
269
+ }
270
+
240
271
if ($ perPage > 0 ) {
241
272
return $ results ->$ paginate ($ perPage , pageName: $ pageName );
242
273
}
0 commit comments