Laravel-Backpack / CRUD

Build custom admin panels. Fast!
https://backpackforlaravel.com
MIT License
3.17k stars 896 forks source link

[Bug] Typed property Backpack\CRUD\app\Library\Database\Table::$columns must not be accessed before initialization #5480

Closed asifroyal closed 7 months ago

asifroyal commented 7 months ago

Bug report

What I did

After updating to Laravel 11 with PHP version 8.2, the following error appears inside the CRUDController function:

 protected function setupCreateOperation()
    {
        CRUD::setValidation(TreatmentRequest::class);

        CRUD::field('name');

        /**
         * Fields can be defined using the fluent syntax or array syntax:
         * - CRUD::field('price')->type('number');
         * - CRUD::addField(['name' => 'price', 'type' => 'number']));
         */
    }

What I expected to happen

??

What happened

[2024-03-26 13:09:57] production.ERROR: Typed property Backpack\CRUD\app\Library\Database\Table::$columns must not be accessed before initialization {"userId":7325,"exception":"[object] (Error(code: 0): Typed property Backpack\\CRUD\\app\\Library\\Database\\Table::$columns must not be accessed before initialization at C:\\laragon\\www\\example\\vendor\\backpack\\crud\\src\\app\\Library\\Database\\Table.php:65)
[stacktrace]
#0 C:\\laragon\\www\\example\\vendor\\backpack\\crud\\src\\app\\Library\\Database\\TableSchema.php(123): Backpack\\CRUD\\app\\Library\\Database\\Table->getColumns()
#1 C:\\laragon\\www\\example\\vendor\\backpack\\crud\\src\\app\\Library\\CrudPanel\\Traits\\AutoSet.php(91): Backpack\\CRUD\\app\\Library\\Database\\TableSchema->getColumns()
#2 C:\\laragon\\www\\example\\vendor\\backpack\\crud\\src\\app\\Library\\CrudPanel\\Traits\\AutoSet.php(56): Backpack\\CRUD\\app\\Library\\CrudPanel\\CrudPanel->getDbTableColumns()
#3 C:\\laragon\\www\\example\\vendor\\backpack\\crud\\src\\app\\Library\\CrudPanel\\Traits\\AutoSet.php(116): Backpack\\CRUD\\app\\Library\\CrudPanel\\CrudPanel->getDbColumnTypes()
#4 C:\\laragon\\www\\example\\vendor\\backpack\\crud\\src\\app\\Library\\CrudPanel\\Traits\\FieldsProtectedMethods.php(227): Backpack\\CRUD\\app\\Library\\CrudPanel\\CrudPanel->inferFieldTypeFromDbColumnType('name')
#5 C:\\laragon\\www\\example\\vendor\\backpack\\crud\\src\\app\\Library\\CrudPanel\\Traits\\Fields.php(55): Backpack\\CRUD\\app\\Library\\CrudPanel\\CrudPanel->makeSureFieldHasType(Array)
#6 C:\\laragon\\www\\example\\vendor\\backpack\\crud\\src\\app\\Library\\CrudPanel\\Traits\\Fields.php(109): Backpack\\CRUD\\app\\Library\\CrudPanel\\CrudPanel->makeSureFieldHasNecessaryAttributes(Array)
#7 C:\\laragon\\www\\example\\vendor\\backpack\\crud\\src\\app\\Library\\CrudPanel\\CrudField.php(406): Backpack\\CRUD\\app\\Library\\CrudPanel\\CrudPanel->addField(Array)
#8 C:\\laragon\\www\\example\\vendor\\backpack\\crud\\src\\app\\Library\\CrudPanel\\CrudField.php(71): Backpack\\CRUD\\app\\Library\\CrudPanel\\CrudField->save()
#9 C:\\laragon\\www\\example\\vendor\\backpack\\crud\\src\\app\\Library\\CrudPanel\\Traits\\Fields.php(575): Backpack\\CRUD\\app\\Library\\CrudPanel\\CrudField->__construct('name')
#10 C:\\laragon\\www\\example\\vendor\\laravel\\framework\\src\\Illuminate\\Support\\Facades\\Facade.php(357): Backpack\\CRUD\\app\\Library\\CrudPanel\\CrudPanel->field('name')
#11 C:\\laragon\\www\\example\\app\\Http\\Controllers\\Admin\\TreatmentCrudController.php(68): Illuminate\\Support\\Facades\\Facade::__callStatic('field', Array)
#12 C:\\laragon\\www\\example\\app\\Http\\Controllers\\Admin\\TreatmentCrudController.php(85): App\\Http\\Controllers\\Admin\\TreatmentCrudController->setupCreateOperation()
#13 C:\\laragon\\www\\example\\vendor\\backpack\\crud\\src\\app\\Http\\Controllers\\CrudController.php(125): App\\Http\\Controllers\\Admin\\TreatmentCrudController->setupUpdateOperation()
#14 C:\\laragon\\www\\example\\vendor\\backpack\\crud\\src\\app\\Http\\Controllers\\CrudController.php(45): Backpack\\CRUD\\app\\Http\\Controllers\\CrudController->setupConfigurationForCurrentOperation()
#15 C:\\laragon\\www\\example\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(165): Backpack\\CRUD\\app\\Http\\Controllers\\CrudController->Backpack\\CRUD\\app\\Http\\Controllers\\{closure}(Object(Illuminate\\Http\\Request), Object(Closure))
#16 C:\\laragon\\www\\example\\app\\Http\\Middleware\\EnsureTokenIsValid.php(35): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#17 C:\\laragon\\www\\example\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(183): App\\Http\\Middleware\\EnsureTokenIsValid->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#18 C:\\laragon\\www\\example\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest.php(21): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#19 C:\\laragon\\www\\example\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Middleware\\ConvertEmptyStringsToNull.php(31): Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#20 C:\\laragon\\www\\example\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(183): Illuminate\\Foundation\\Http\\Middleware\\ConvertEmptyStringsToNull->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#21 C:\\laragon\\www\\example\\app\\Http\\Middleware\\CheckIfAdmin.php(65): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#22 C:\\laragon\\www\\example\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(183): App\\Http\\Middleware\\CheckIfAdmin->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#23 C:\\laragon\\www\\example\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Middleware\\SubstituteBindings.php(50): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#24 C:\\laragon\\www\\example\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(183): Illuminate\\Routing\\Middleware\\SubstituteBindings->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#25 C:\\laragon\\www\\example\\vendor\\backpack\\crud\\src\\app\\Http\\Middleware\\AuthenticateSession.php(62): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#26 C:\\laragon\\www\\example\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(183): Backpack\\CRUD\\app\\Http\\Middleware\\AuthenticateSession->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#27 C:\\laragon\\www\\example\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Middleware\\VerifyCsrfToken.php(88): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#28 C:\\laragon\\www\\example\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(183): Illuminate\\Foundation\\Http\\Middleware\\VerifyCsrfToken->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#29 C:\\laragon\\www\\example\\vendor\\laravel\\framework\\src\\Illuminate\\View\\Middleware\\ShareErrorsFromSession.php(49): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#30 C:\\laragon\\www\\example\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(183): Illuminate\\View\\Middleware\\ShareErrorsFromSession->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#31 C:\\laragon\\www\\example\\vendor\\laravel\\framework\\src\\Illuminate\\Session\\Middleware\\StartSession.php(121): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#32 C:\\laragon\\www\\example\\vendor\\laravel\\framework\\src\\Illuminate\\Session\\Middleware\\StartSession.php(64): Illuminate\\Session\\Middleware\\StartSession->handleStatefulRequest(Object(Illuminate\\Http\\Request), Object(Illuminate\\Session\\Store), Object(Closure))
#33 C:\\laragon\\www\\example\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(183): Illuminate\\Session\\Middleware\\StartSession->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#34 C:\\laragon\\www\\example\\vendor\\laravel\\framework\\src\\Illuminate\\Cookie\\Middleware\\AddQueuedCookiesToResponse.php(37): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#35 C:\\laragon\\www\\example\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(183): Illuminate\\Cookie\\Middleware\\AddQueuedCookiesToResponse->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#36 C:\\laragon\\www\\example\\vendor\\laravel\\framework\\src\\Illuminate\\Cookie\\Middleware\\EncryptCookies.php(75): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#37 C:\\laragon\\www\\example\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(183): Illuminate\\Cookie\\Middleware\\EncryptCookies->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#38 C:\\laragon\\www\\example\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(119): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#39 C:\\laragon\\www\\example\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Router.php(805): Illuminate\\Pipeline\\Pipeline->then(Object(Closure))
#40 C:\\laragon\\www\\example\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Router.php(784): Illuminate\\Routing\\Router->runRouteWithinStack(Object(Illuminate\\Routing\\Route), Object(Illuminate\\Http\\Request))
#41 C:\\laragon\\www\\example\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Router.php(748): Illuminate\\Routing\\Router->runRoute(Object(Illuminate\\Http\\Request), Object(Illuminate\\Routing\\Route))
#42 C:\\laragon\\www\\example\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Router.php(737): Illuminate\\Routing\\Router->dispatchToRoute(Object(Illuminate\\Http\\Request))
#43 C:\\laragon\\www\\example\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Kernel.php(200): Illuminate\\Routing\\Router->dispatch(Object(Illuminate\\Http\\Request))
#44 C:\\laragon\\www\\example\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(144): Illuminate\\Foundation\\Http\\Kernel->Illuminate\\Foundation\\Http\\{closure}(Object(Illuminate\\Http\\Request))
#45 C:\\laragon\\www\\example\\vendor\\livewire\\livewire\\src\\Features\\SupportDisablingBackButtonCache\\DisableBackButtonCacheMiddleware.php(19): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#46 C:\\laragon\\www\\example\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(183): Livewire\\Features\\SupportDisablingBackButtonCache\\DisableBackButtonCacheMiddleware->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#47 C:\\laragon\\www\\example\\vendor\\barryvdh\\laravel-debugbar\\src\\Middleware\\InjectDebugbar.php(59): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#48 C:\\laragon\\www\\example\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(183): Barryvdh\\Debugbar\\Middleware\\InjectDebugbar->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#49 C:\\laragon\\www\\example\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest.php(21): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#50 C:\\laragon\\www\\example\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Middleware\\ConvertEmptyStringsToNull.php(31): Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#51 C:\\laragon\\www\\example\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(183): Illuminate\\Foundation\\Http\\Middleware\\ConvertEmptyStringsToNull->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#52 C:\\laragon\\www\\example\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest.php(21): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#53 C:\\laragon\\www\\example\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Middleware\\TrimStrings.php(50): Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#54 C:\\laragon\\www\\example\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(183): Illuminate\\Foundation\\Http\\Middleware\\TrimStrings->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#55 C:\\laragon\\www\\example\\vendor\\laravel\\framework\\src\\Illuminate\\Http\\Middleware\\ValidatePostSize.php(27): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#56 C:\\laragon\\www\\example\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(183): Illuminate\\Http\\Middleware\\ValidatePostSize->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#57 C:\\laragon\\www\\example\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Middleware\\PreventRequestsDuringMaintenance.php(110): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#58 C:\\laragon\\www\\example\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(183): Illuminate\\Foundation\\Http\\Middleware\\PreventRequestsDuringMaintenance->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#59 C:\\laragon\\www\\example\\vendor\\laravel\\framework\\src\\Illuminate\\Http\\Middleware\\HandleCors.php(49): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#60 C:\\laragon\\www\\example\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(183): Illuminate\\Http\\Middleware\\HandleCors->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#61 C:\\laragon\\www\\example\\vendor\\laravel\\framework\\src\\Illuminate\\Http\\Middleware\\TrustProxies.php(57): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#62 C:\\laragon\\www\\example\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(183): Illuminate\\Http\\Middleware\\TrustProxies->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#63 C:\\laragon\\www\\example\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(119): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#64 C:\\laragon\\www\\example\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Kernel.php(175): Illuminate\\Pipeline\\Pipeline->then(Object(Closure))
#65 C:\\laragon\\www\\example\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Kernel.php(144): Illuminate\\Foundation\\Http\\Kernel->sendRequestThroughRouter(Object(Illuminate\\Http\\Request))
#66 C:\\laragon\\www\\example\\public\\index.php(51): Illuminate\\Foundation\\Http\\Kernel->handle(Object(Illuminate\\Http\\Request))
#67 {main}
"} 

Is it a bug in the latest version of Backpack?

After I run composer update backpack/crud the bug... is it still there?

Yes

Backpack, Laravel, PHP, DB version

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

PHP VERSION:

PHP 8.2.0 (cli) (built: Dec  6 2022 15:31:23) (ZTS Visual C++ 2019 x64)
Copyright (c) The PHP Group
Zend Engine v4.2.0, Copyright (c) Zend Technologies

### LARAVEL VERSION:
11.0.8.0

### BACKPACK PACKAGE VERSIONS:
backpack/basset: 1.3.0
backpack/crud: 6.7.2
backpack/generators: v4.0.4
backpack/permissionmanager: 7.2.0
backpack/pro: 2.1.11
backpack/theme-coreuiv2: 1.2.3
pxpm commented 7 months ago

Hey @asifroyal thanks for the report.

I've already merged a PR #5481 in CRUD to fix this issue, I will tag a new version in a couple hours when I get to merge some other fixes I have planned.

In the meanwhile I am curious, how you were able to trigger the error ?

Mysql and Sqlite doesn't allow you to create empty tables, so in my head you shouldn't be getting there without atleast one column.

Apparently after some research I found that is possible in pgsql. Is that the case here ?

I will be closing this issue, let me know if you experience issues after I release the new CRUD version later today.

Cheers

asifroyal commented 7 months ago

Hey @asifroyal thanks for the report.

I've already merged a PR #5481 in CRUD to fix this issue, I will tag a new version in a couple hours when I get to merge some other fixes I have planned.

In the meanwhile I am curious, how you were able to trigger the error ?

Mysql and Sqlite doesn't allow you to create empty tables, so in my head you shouldn't be getting there without atleast one column.

Apparently after some research I found that is possible in pgsql. Is that the case here ?

I will be closing this issue, let me know if you experience issues after I release the new CRUD version later today.

Cheers

Yes you are correct. I am using pgsql.