Skip to content

Instantly share code, notes, and snippets.

@julienbourdeau
Created September 27, 2017 16:06
Show Gist options
  • Save julienbourdeau/c5b3adc369da2e5e02527de3cb73ee6c to your computer and use it in GitHub Desktop.
Save julienbourdeau/c5b3adc369da2e5e02527de3cb73ee6c to your computer and use it in GitHub Desktop.
[Laravel Scout] Clear index custom command
<?php
namespace App\Console\Commands;
use AlgoliaSearch\Client;
use Illuminate\Console\Command;
class ClearIndexCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'scout:clear {model}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Clear all data in search index, regardless of database';
/**
* Execute the console command.
*
* @return void
*/
public function handle()
{
$class = $this->argument('model');
$model = new $class;
$algolia = new Client(config('scout.algolia.id'), config('scout.algolia.secret'));
$index = $algolia->initIndex($model->searchableAs());
// Remember this is an asynchronous operation in Algolia
$index->clearIndex();
$this->info('Index '.$model->searchableAs().' clearing.');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment