sources api
This commit is contained in:
parent
1c090d9293
commit
7bea612e41
|
|
@ -0,0 +1,70 @@
|
|||
<?php
|
||||
|
||||
namespace Sarga\Admin\Console\Commands;
|
||||
|
||||
use Illuminate\Console\Command;
|
||||
use MeiliSearch\Client;
|
||||
use function PHPUnit\Framework\throwException;
|
||||
|
||||
class UpdateMeilisearchIndex extends Command
|
||||
{
|
||||
/**
|
||||
* The name and signature of the console command.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $signature = 'meilisearch:update';
|
||||
|
||||
/**
|
||||
* The console command description.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Update Meilisearch\'s index and filterable attributes';
|
||||
|
||||
/**
|
||||
* Create a new command instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the console command.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$client = new Client(config('scout.meilisearch.host'));
|
||||
|
||||
$this->updateSortableAttributes($client);
|
||||
|
||||
$this->updateFilterableAttributes($client);
|
||||
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
|
||||
protected function updateSortableAttributes(Client $client):void
|
||||
{
|
||||
$client->index('products_index')->updateSortableAttributes([
|
||||
'status',
|
||||
'visible_individually',
|
||||
]);
|
||||
|
||||
$this->info('Updated sortable attributes...');
|
||||
}
|
||||
|
||||
protected function updateFilterableAttributes(Client $client): void
|
||||
{
|
||||
$client->index('products_index')->updateFilterableAttributes([
|
||||
'name',
|
||||
'product_id',
|
||||
]);
|
||||
|
||||
$this->info('Updated filterable attributes...');
|
||||
}
|
||||
}
|
||||
|
|
@ -4,6 +4,7 @@ namespace Sarga\Admin\Providers;
|
|||
|
||||
use Illuminate\Routing\Router;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
use Sarga\Admin\Console\Commands\UpdateMeilisearchIndex;
|
||||
|
||||
class AdminServiceProvider extends ServiceProvider
|
||||
{
|
||||
|
|
@ -46,5 +47,9 @@ class AdminServiceProvider extends ServiceProvider
|
|||
$this->mergeConfigFrom(
|
||||
dirname(__DIR__) . '/Config/carriers.php', 'carriers'
|
||||
);
|
||||
|
||||
$this->commands([
|
||||
UpdateMeilisearchIndex::class
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -53,13 +53,13 @@ class ProductFlat extends Model implements ProductFlatContract
|
|||
{
|
||||
return 'products_index';
|
||||
}
|
||||
public function toSearchableArray()
|
||||
public function toSearchableArray():array
|
||||
{
|
||||
$array = $this->toArray();
|
||||
|
||||
// Customize the data array...
|
||||
|
||||
return $array;
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'name' => $this->name,
|
||||
'meta_keywords' => $this->meta_keywords,
|
||||
];
|
||||
}
|
||||
/**
|
||||
* Get an attribute from the model.
|
||||
|
|
|
|||
Loading…
Reference in New Issue