Created
September 27, 2017 16:06
-
-
Save julienbourdeau/c5b3adc369da2e5e02527de3cb73ee6c to your computer and use it in GitHub Desktop.
[Laravel Scout] Clear index custom command
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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