Generators: [Bug] Problem adding CrudTrait to existing Model

Bug report

What I did

After building a draft.yml for the Customer model:

php artisan blueprint:build 
php artisan backpack:crud Customer

What I expected to happen

I expected (1) the CrudTrait to be added to the existing Model, and (2) the setUpCreateOperation() and setUpListOperation() to be filled in.

What happened

PS C:\Apps\xampp\htdocs\cofour-intern>  php artisan blueprint:build 
...
PS C:\Apps\xampp\htdocs\cofour-intern> php artisan backpack:crud Customer     
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> 

What I’ve already tried to fix it

I manually added use Backpack\CRUD\app\Models\Traits\CrudTrait; to the model, and use CrudTrait; to the class body, which auto-generated the proper files (expectation 1), but does not fill out the setUpOperations (expectation 2).

Also described here on Stackoverflow.

Backpack, Laravel, PHP, DB version

When I run php artisan backpack:version the output is:

### PHP VERSION:
PHP 7.4.7 (cli) (built: Jun  9 2020 13:36:15) ( ZTS Visual C++ 2017 x64 )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies

### LARAVEL VERSION:
v7.19.0@67d6d9688122dcf6c449b74ddc873fc9934e81e6

### BACKPACK VERSION:
4.1.14@f4a6ad91f2aa3760ffe963c5cd87d2b533803323 

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 19 (7 by maintainers)

Most upvoted comments

@tabacitu I’ve found the bug causing the problem reported by @yabbou https://github.com/Laravel-Backpack/Generators/issues/95#issuecomment-665967884, and I’ve just opened a PR for that https://github.com/Laravel-Backpack/Generators/pull/104.

I think this issue is no longer a problem, I’ll close it for now. Let’s move the conversation to https://github.com/Laravel-Backpack/Generators/pull/104

@promatik I also noticed this problem this week. I generated a few models and the Generators didn’t fill in the column & fields - so I think there’s truly a bug here - and big one.

Do you mind investigating this please and create a PR if possible? If it doesn’t fill in the fields&columns, this package only provides a minor boost in productivity so it’s pretty big problem.

@yabbou you’ll need to run

  • composer update backpack/crud to update Backpack CRUD
  • composer update backpack/generators to update Backpack Generators

Please give it a try and let us know the result 🙌

Hi @yabbou, I tried to generate a model with blueprint and then build backpack, and the model got correctly fixed with the CrudTrait. Do you still happen to have this problem?

Hello @yabbou

I just moved your issue from crud repository to generators, I think here is the correct place for it.

The error states that the controller already exists. Did you try deleting it and running the command again ?

Can you make sure you are using the last version of generators ? It’s 3.1.3 atm.

Best, Pedro