Skip to content

Commit

Permalink
Feature - Add scout searching to pages with searchable models. (#1445)
Browse files Browse the repository at this point in the history
Co-authored-by: Glenn Jacobs <glenn@neondigital.co.uk>
  • Loading branch information
alecritson and glennjacobs authored Jan 4, 2024
1 parent 92ebaa4 commit 418541e
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 2 deletions.
14 changes: 14 additions & 0 deletions packages/admin/config/search.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

return [
/*
|--------------------------------------------------------------------------
| Enable Scout when searching on supported models.
|--------------------------------------------------------------------------
|
| Some models in the core have Scout implemented as a search driver, if you
| want to use Scout when possible on tables in the panel, enable it here.
|
*/
'scout_enabled' => true,
];
2 changes: 1 addition & 1 deletion packages/admin/src/Filament/Resources/BrandResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public static function getDefaultTable(Table $table): Table
Tables\Actions\BulkActionGroup::make([
Tables\Actions\DeleteBulkAction::make(),
]),
]);
])->searchable();
}

protected static function getTableColumns(): array
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Lunar\Admin\Filament\Resources\OrderResource\Pages;

use Filament\Actions;
use Filament\Resources\Components\Tab;
use Illuminate\Contracts\Pagination\Paginator;
use Illuminate\Database\Eloquent\Builder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ public static function getDefaultTable(Table $table): Table
Tables\Actions\DeleteBulkAction::make(),
]),
])
->searchable()
->defaultSort('position', 'asc')
->reorderable('position');
}
Expand Down
10 changes: 10 additions & 0 deletions packages/admin/src/LunarPanelProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@

class LunarPanelProvider extends ServiceProvider
{
protected $configFiles = [
'search',
];

protected $root = __DIR__.'/..';

public function register(): void
{
$this->app->scoped('lunar-panel', function (): LunarPanelManager {
Expand Down Expand Up @@ -54,6 +60,10 @@ public function boot(): void
__DIR__.'/../resources/lang' => $this->app->langPath('vendor/lunarpanel'),
]);

collect($this->configFiles)->each(function ($config) {
$this->mergeConfigFrom("{$this->root}/config/$config.php", "lunar.$config");
});

$this->publishes([
__DIR__.'/../public' => public_path('vendor/lunarpanel'),
], 'public');
Expand Down
29 changes: 29 additions & 0 deletions packages/admin/src/Support/Pages/BaseListRecords.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,38 @@
namespace Lunar\Admin\Support\Pages;

use Filament\Resources\Pages\ListRecords;
use Illuminate\Database\Eloquent\Builder;
use Lunar\Base\Traits\Searchable;

abstract class BaseListRecords extends ListRecords
{
use Concerns\ExtendsHeaderActions;
use \Lunar\Admin\Support\Concerns\CallsHooks;

protected function applySearchToTableQuery(Builder $query): Builder
{
$scoutEnabled = config('lunar.search.scout_enabled', false);
$isScoutSearchable = in_array(Searchable::class, class_uses_recursive(static::getModel()));

$this->applyColumnSearchesToTableQuery($query);

if (! $scoutEnabled || ! $isScoutSearchable) {
$this->applyGlobalSearchToTableQuery($query);
}

if (
filled($search = $this->getTableSearch()) &&
$scoutEnabled &&
$isScoutSearchable
) {
$query->whereIn(
'id',
collect(static::getModel()::search($search)->keys())->map(
fn ($result) => str_replace(static::getModel().'::', '', $result)
)
);
}

return $query;
}
}
4 changes: 4 additions & 0 deletions packages/core/src/Search/ScoutIndexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ protected function mapSearchableAttributes(Model $model): array

$attributeData = $model->attribute_data;

if (! $attributeData) {
return [];
}

$data = [];

foreach ($attributes as $attribute) {
Expand Down

0 comments on commit 418541e

Please sign in to comment.