将 CrudTrait 添加到现有模型时出现问题

Problem adding CrudTrait to existing Model

使用 Backpack 为项目做事(自动添加其功能)时出现问题。

来自tutorial:

我的:

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> 

注释掉 CrudTrait 的自动添加,然后按照@OMR here 的建议手动添加,有效。大概不需要,但现在命令调用按常规顺序工作:

blueprint:build
backpack:build

CrudModelBackpackCommand.php(第 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;
    }

我可能移动了一些文件,但我不确定是哪个...

这个案例也作为他们 github here.

上的错误打开

转到您的模型和使用部分添加:

use Backpack\CRUD\app\Models\Traits\CrudTrait;

并在模型主体部分添加:

use CrudTrait;

应该可以。