在将CrudTrait添加到现有模型时出现问题 [英] Problem adding CrudTrait to existing Model

查看:46
本文介绍了在将CrudTrait添加到现有模型时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用背包在项目中完成其任务(自动添加其功能)的问题.

矿山:

  PS C:\ Apps \ xampp \ htdocs \ cofour-intern>php artisan蓝图:build...PS C:\ Apps \ xampp \ htdocs \ cofour-intern>php artisan背包:crud产品控制器创建成功.ErrorException未定义的变量:位置在C:\ Apps \ xampp \ htdocs \ cofour-intern \ vendor \ backpack \ generators \ src \ Console \ Commands \ CrudModelBackpackCommand.php:9490 |//与数组索引相同-数组从0开始计数,91 |//IDE从1开始计数92 |93 |//添加CrudTrait>94 |array_splice($ file_array,$ position,0,'使用\\'.$ this-> crudTrait.';');95 |96 |//保存文件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:94Illuminate \ Foundation \ Bootstrap \ HandleExceptions :: handleError(未定义变量:位置","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:33Backpack \ Generators \ Console \ Commands \ CrudModelBackpackCommand :: handle()PS C:\ Apps \ xampp \ htdocs \ cofour-intern> 

解决方案

注释自动添加CrudTrait,然后按照@OMR的建议手动添加这里.

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

From the tutorial:

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> 

解决方案

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.

这篇关于在将CrudTrait添加到现有模型时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆