Skip to content

Commit

Permalink
Add Scout Builder support
Browse files Browse the repository at this point in the history
  • Loading branch information
luanfreitasdev committed May 31, 2024
1 parent 9125e36 commit 29f0ad9
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 14 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
"larastan/larastan": "^2.9.0",
"pestphp/pest": "^2.34.0",
"orchestra/testbench": "8.19|^9.0",
"laradumps/laradumps": "^3.1"
"laradumps/laradumps": "^3.1",
"laravel/scout": "^10.9"
},
"suggest": {
"openspout/openspout": "Required to export XLS and CSV"
Expand Down
55 changes: 43 additions & 12 deletions src/ProcessDataSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
use Illuminate\Database\Eloquent\{Builder as EloquentBuilder, Model};
use Illuminate\Database\Query\Builder as QueryBuilder;
use Illuminate\Pagination\Paginator;
use Illuminate\Support\{Collection as BaseCollection, Facades\DB, Str};
use Illuminate\Support\{Collection as BaseCollection, Facades\DB, Str, Stringable};
use Laravel\Scout\Builder as ScoutBuilder;
use PowerComponents\LivewirePowerGrid\Components\Actions\ActionsController;
use PowerComponents\LivewirePowerGrid\Components\Rules\{RulesController};
use PowerComponents\LivewirePowerGrid\DataSource\{Builder, Collection};
Expand All @@ -18,8 +19,6 @@ class ProcessDataSource
{
use Concerns\SoftDeletes;

public bool $isCollection = false;

private array $queryLog = [];

public function __construct(
Expand Down Expand Up @@ -49,16 +48,46 @@ public function get(bool $isExport = false): Paginator|LengthAwarePaginator|\Ill
return $this->processCollection($datasource, $isExport);
}

if ($datasource instanceof ScoutBuilder) {
return $this->processScoutCollection($datasource);
}

$this->setCurrentTable($datasource);

/** @phpstan-ignore-next-line */
return $this->processModel($datasource);
return $this->processModel($datasource); // @phpstan-ignore-line
}

/**
* @return EloquentBuilder|BaseCollection|Collection|QueryBuilder|MorphToMany|null
*/
public function prepareDataSource(): EloquentBuilder|BaseCollection|Collection|QueryBuilder|MorphToMany|null
public function processScoutCollection(ScoutBuilder $datasource): Paginator|LengthAwarePaginator
{
$datasource->query = Str::of($datasource->query)
->when($this->component->search != '', fn (Stringable $self) => $self
->prepend($this->component->search . ','))
->toString();

collect($this->component->filters)->each(fn (array $filters) => collect($filters)
->each(fn (string $value, string $field) => $datasource
->where($field, $value)));

if ($this->component->multiSort) {
foreach ($this->component->sortArray as $sortField => $direction) {
$datasource->orderBy($sortField, $direction);
}
} else {
$datasource->orderBy($this->component->sortField, $this->component->sortDirection);
}

$results = self::applyPerPage($datasource);

if (method_exists($results, 'total')) {
$this->component->total = $results->total();
}

return $results->setCollection( // @phpstan-ignore-line
$this->transform($results->getCollection(), $this->component) // @phpstan-ignore-line
);
}

public function prepareDataSource(): EloquentBuilder|BaseCollection|Collection|QueryBuilder|MorphToMany|ScoutBuilder|null
{
$datasource = $this->component->datasource ?? null;

Expand All @@ -70,8 +99,6 @@ public function prepareDataSource(): EloquentBuilder|BaseCollection|Collection|Q
$datasource = collect($datasource);
}

$this->isCollection = $datasource instanceof BaseCollection;

return $datasource;
}

Expand Down Expand Up @@ -226,7 +253,7 @@ private function applyWithSortStringNumber(
return $results;
}

private function applyPerPage(EloquentBuilder|QueryBuilder|MorphToMany $results): LengthAwarePaginator|Paginator
private function applyPerPage(EloquentBuilder|QueryBuilder|MorphToMany|ScoutBuilder $results): LengthAwarePaginator|Paginator
{
$pageName = strval(data_get($this->component->setUp, 'footer.pageName', 'page'));
$perPage = intval(data_get($this->component->setUp, 'footer.perPage'));
Expand All @@ -237,6 +264,10 @@ private function applyPerPage(EloquentBuilder|QueryBuilder|MorphToMany $results)
default => 'paginate',
};

if ($results instanceof ScoutBuilder) {
return $results->paginateSafe($perPage, pageName: $pageName); // @phpstan-ignore-line
}

if ($perPage > 0) {
return $results->$paginate($perPage, pageName: $pageName);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Traits/WithExport.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ public function prepareToExport(bool $selected = false): Eloquent\Collection|Sup
$inClause = $processDataSource->component->checkboxValues;
}

if ($processDataSource->isCollection) {
if ($processDataSource->component->datasource() instanceof Collection) {
if ($inClause) {
$results = $processDataSource->get(isExport: true)->whereIn($this->primaryKey, $inClause);

Expand Down

0 comments on commit 29f0ad9

Please sign in to comment.