99/** 
1010 * Class RebuildIndex 
1111 * 
12-  * This  Artisan command rebuilds the encrypted search index for a given Eloquent model. 
13-  * It is designed for maintenance operations where the search index may be outdated,  
14-  * corrupted, or needs regeneration after schema or normalization changes . 
12+  * Artisan command that  rebuilds the encrypted search index for a given Eloquent model. 
13+  * It now supports short model names (e.g. "Client") and automatically resolves them  
14+  * under the `App\Models` namespace if not fully qualified . 
1515 * 
16-  * The command iterates over all records of the specified model and regenerates 
17-  * both "exact" and "prefix" tokens as defined by the model’s 
18-  * `HasEncryptedSearchIndex` trait configuration. 
19-  * 
20-  * Usage example: 
16+  * Example: 
17+  *   php artisan encryption:index-rebuild Client 
2118 *   php artisan encryption:index-rebuild "App\Models\Client" 
22-  * 
23-  * Options: 
24-  *   --chunk=100   Number of records processed per batch (default: 100) 
25-  * 
26-  * Implementation details: 
27-  * - Before rebuilding, all existing search index entries for the given model 
28-  *   are removed. 
29-  * - Records are then reprocessed in chunks to prevent memory exhaustion. 
30-  * - For each model instance, `updateSearchIndex()` is called to regenerate 
31-  *   the normalized token rows. 
32-  * 
33-  * This ensures a clean and consistent index aligned with the current model data. 
3419 */ 
3520class  RebuildIndex extends  Command
3621{
3722    /** 
3823     * The name and signature of the console command. 
3924     * 
40-      * {model}  The fully qualified class name (FQCN) of the Eloquent model. 
41-      * {--chunk=100}  The number of model records to process per batch. 
42-      * 
4325     * @var string 
4426     */ 
45-     protected  $ signature'encryption:index-rebuild {model : FQCN of the Eloquent model} {--chunk=100} ' ;
27+     protected  $ signature'encryption:index-rebuild  
28+         {model : Model name or FQCN of the Eloquent model} 
29+         {--chunk=100 : Number of records processed per batch} '
4630
4731    /** 
4832     * The console command description. 
@@ -54,49 +38,54 @@ class RebuildIndex extends Command
5438    /** 
5539     * Execute the console command. 
5640     * 
57-      * This method performs the following steps: 
58-      *  1. Validates the provided model class. 
59-      *  2. Deletes all existing search index entries for that model. 
60-      *  3. Iterates through all model records in configurable chunks. 
61-      *  4. Calls `updateSearchIndex()` for each record to regenerate tokens. 
62-      *  5. Displays progress and a final summary of processed records. 
63-      * 
64-      * @return int Command exit code (0 on success, 1 on failure). 
41+      * @return int 
6542     */ 
6643    public  function  handle (): int 
6744    {
45+         $ inputtrim ($ this argument ('model ' ));
46+ 
47+         // Automatically resolve models under App\Models namespace if not fully qualified 
48+         if  (! class_exists ($ input
49+             $ guessed"App \\Models \\{$ input ;
50+             if  (class_exists ($ guessed
51+                 $ input$ guessed
52+             }
53+         }
54+ 
6855        /** @var class-string<Model> $class */ 
69-         $ class$ this -> argument ( ' model ' ) ;
56+         $ class$ input 
7057
7158        if  (! class_exists ($ class
72-             $ this error ("Model class not found:  {$ class  );
59+             $ this error ("Model class not found:  {$ this -> argument ( ' model ' ) }" );
7360            return  self ::FAILURE ;
7461        }
7562
7663        $ chunkint ) $ this option ('chunk ' );
64+         $ this info ("Rebuilding encrypted search index for:  {$ class );
65+         $ this line ("Processing in chunks of  {$ chunk... " );
7766
7867        // Remove all existing search tokens for this model 
7968        SearchIndex::where ('model_type ' , $ classdelete ();
8069
81-         /** @var \Illuminate\Database\Eloquent\Builder $q  */ 
82-         $ q $ classquery ();
70+         /** @var \Illuminate\Database\Eloquent\Builder $query  */ 
71+         $ query $ classquery ();
8372
8473        $ count0 ;
8574
86-         // Process model data in chunks to minimize memory usage 
87-         $ qchunk ($ chunkfunction  ($ rowsuse  (&$ count$ class
88-             foreach  ($ rowsas  $ model
89-                 if  (method_exists ($ class'updateSearchIndex ' )) {
90-                     $ classupdateSearchIndex ($ model
75+         $ querychunk ($ chunkfunction  ($ modelsuse  (&$ count
76+             foreach  ($ modelsas  $ model
77+                 if  (method_exists ($ model'updateSearchIndex ' )) {
78+                     $ modelupdateSearchIndex (); // <-- FIXED 
9179                }
9280                $ count
9381            }
82+ 
9483            // Write a dot to indicate progress 
9584            $ this output ->write ('. ' );
9685        });
9786
9887        $ this newLine ();
99-         $ this info ("Rebuilt index for  {$ count records of  {$ class. " );
88+         $ this info ("✅  Rebuilt index for $ count records of  {$ class. " );
10089
10190        return  self ::SUCCESS ;
10291    }
0 commit comments