laravelmodellaravel-backpack

Problem adding CrudTrait to existing Model


Problem using Backpack to do its thing (automate adding its features) to the project.

From the tutorial:

enter image description here

Mine:

PS C:\Apps\xampp\htdocs\cofour-intern>  php artisan blueprint:build 
...
PS C:\Apps\xampp\htdocs\cofour-intern> php artisan backpack:crud Product     
Controller created successfully.

   ErrorException 

  Undefined variable: position

  at C:\Apps\xampp\htdocs\cofour-intern\vendor\backpack\generators\src\Console\Commands\CrudModelBackpackCommand.php:94
    90|                     // the same as the array index - arrays start counting from 0,
    91|                     // IDEs start counting from 1
    92| 
    93|                     // add CrudTrait
  > 94|                     array_splice($file_array, $position, 0, '    use \\'.$this->crudTrait.';');
    95| 
    96|                     // save the file
    97|                     $this->files->put($path, implode(PHP_EOL, $file_array));
    98| 

  1   C:\Apps\xampp\htdocs\cofour-intern\vendor\backpack\generators\src\Console\Commands\CrudModelBackpackCommand.php:94
      Illuminate\Foundation\Bootstrap\HandleExceptions::handleError("Undefined variable: position", "C:\Apps\xampp\htdocs\cofour-intern\vendor\backpack\generators\src\Console\Commands\CrudModelBackpackCommand.php")

  2   C:\Apps\xampp\htdocs\cofour-intern\vendor\laravel\framework\src\Illuminate\Container\BoundMethod.php:33     
      Backpack\Generators\Console\Commands\CrudModelBackpackCommand::handle()
PS C:\Apps\xampp\htdocs\cofour-intern> 

Solution

  • Commenting out the automated adding of CrudTrait, and then adding it manually as recommended by @OMR here, works. Presumably should not be necessary, but now the command calls work in the regular order:

    blueprint:build
    backpack:build
    

    CrudModelBackpackCommand.php (line 75):

            // if it does not have CrudTrait, add the trait on the Model
    
            $classDefinition = 'class '.$this->getNameInput().' extends';
    
            // foreach ($file_array as $key => $line) {
            //     if (Str::contains($line, $classDefinition)) {
            //         if (Str::endsWith($line, '{')) {
            //             // add the trait on the next
            //             $position = $key + 1;
            //         } elseif ($file_array[$key + 1] == '{') {
            //             // add the trait on the next next line
            //             $position = $key + 2;
            //         }
    
            //         // keep in mind that the line number shown in IDEs is not
            //         // the same as the array index - arrays start counting from 0,
            //         // IDEs start counting from 1
    
            //         // add CrudTrait
            //         array_splice($file_array, $position, 0, '    use \\'.$this->crudTrait.';');
    
            //         // save the file
            //         $this->files->put($path, implode(PHP_EOL, $file_array));
    
            //         // let the user know what we've done
            //         $this->info('Model already exists! We just added CrudTrait on it.');
    
            //         return false;
            //     }
            // }
    
            $this->error('Model already exists! Could not add CrudTrait - please add manually.');
    
            return false;
        }
    

    I likely moved some file out of place, but I am not sure which one...

    The case has also been opened as a bug on their github here.