Initial commit
This commit is contained in:
65
app/Console/Commands/ScoutReindexCommand.php
Normal file
65
app/Console/Commands/ScoutReindexCommand.php
Normal file
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
|
||||
// Create this file: app/Console/Commands/ScoutReindexCommand.php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use Illuminate\Console\Command;
|
||||
|
||||
class ScoutReindexCommand extends Command
|
||||
{
|
||||
/**
|
||||
* The name and signature of the console command.
|
||||
*/
|
||||
protected $signature = 'scout:reindex-model
|
||||
{model : The model class name (e.g., User, Organization)}
|
||||
{--id= : Specific model ID to reindex}';
|
||||
|
||||
/**
|
||||
* The console command description.
|
||||
*/
|
||||
protected $description = 'Manually trigger Scout reindexing for specific models';
|
||||
|
||||
/**
|
||||
* Execute the console command.
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$modelName = $this->argument('model');
|
||||
$modelId = $this->option('id');
|
||||
|
||||
// Build full model class name
|
||||
$modelClass = "App\\Models\\{$modelName}";
|
||||
|
||||
if (!class_exists($modelClass)) {
|
||||
$this->error("Model {$modelClass} does not exist.");
|
||||
return 1;
|
||||
}
|
||||
|
||||
try {
|
||||
if ($modelId) {
|
||||
// Reindex specific model
|
||||
$model = $modelClass::find($modelId);
|
||||
if (!$model) {
|
||||
$this->error("Model {$modelName} with ID {$modelId} not found.");
|
||||
return 1;
|
||||
}
|
||||
|
||||
$this->info("Reindexing {$modelName} #{$modelId}...");
|
||||
|
||||
// Force reindex the model
|
||||
$model->searchable();
|
||||
|
||||
$this->info("✅ Successfully reindexed {$modelName} #{$modelId}");
|
||||
} else {
|
||||
$this->error("Please specify --id=X");
|
||||
return 1;
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
$this->error("Failed to reindex: " . $e->getMessage());
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user