v1.x Upgrade Guide
Preface
This section contains only the 1.x version of the API changes, does not contain new features or changes that do not affect the user’s use, 2.0 specific version change description please refer to [what changes in 2.0?] (https://learnku.com/articles/50781?#reply164307)
Estimated upgrade time: 60 minutes
1. Create a new branch and backup configuration files
Create a new branch and backup the configuration file config/admin.php named config/admin.bak.php to facilitate subsequent comparison of configuration changes.
2. Update composer dependencies
Uninstall 1.x version first
composer remove dcat/laravel-admin
Re-installation
composer require dcat/laravel-admin:"2.*"
After installing success
- delete the
public/vendorsdirectory - republish the resource
php artisan admin:publish --force. - write the modified parameters to the new configuration file
config/admin.phpaccording to the above backed up configuration file, here it should be noted that the default theme color for1.xisindigo(deprecated), which has been replaced withdefaultin the new version - Adjust the language pack, the language pack directory is changed from
zh-CNtozh_CNin the new version, you need to move the custom translation files to the new directory, and the translation ofmenuTITLEis also separated intomenus.php. - Run the database migration command
php artisan migrate, two new tablesadmin_settingsandadmin_extensionshave been added in the new version
3. Global change namespace
- Global search namespace
Dcat\Admin\Controllersand replace it withDcat\Admin\Http\Controllers - global search namespace
Dcat\Admin\Authand replace it with `Dcat\Admin\Http\Auth
4. Form section changes
- The field hiding function has been adjusted, the
responsivemethod has been deprecated in the old version, in the new version the field hiding function is enabled as follows
// Turn on the field selector function$grid->showColumnSelector();// Set default hidden fields$grid->hideColumns(['field1', ...]) ;
- The methods
collectionandfetchinghave been removed from the table and can be replaced in the new version by the following events
use Dcat\Admin\Grid;use Illuminate\Support\Collection;// Use Grid\Events\Fetched event instead of collection$grid->listen(Grid\Events\Fetched::class, function ($grid, Collection $rows) {$rows->transform(function ($row) {// Changing row data$row['name'] = $row['first_name'].' '.$row['last_name'];return $row;});});// Use Grid\Events\Fetching event instead of fetching$grid->listen(Grid\Events\Fetching::class, function ($grid) {});
- The use of models is allowed in the table row closure function
$grid->column('avatar')->display(function ({// Direct access to model-related methodsreturn $this->getAvatar();});
Set route prefix method from
resourcetosetResource.$grid->setResource('auth/users');
Tree form
treemethod will be deprecated soon and will be moved to extension center
5. Changes to the form section
- Adjusted form handling response methods,
success,error,redirectandlocationmethods have been removed from the old version. In2.0we unified the form response methods with theactionresponse methods, please refer to the document form response, Example for detailed Usage
$form->saving(function (Form $form) {return $form->response()->success('Save success')->script('console.log("Execution of JS code")')->redirect('auth/users');});
In the case of tools-form, the Usage is as follows
public function handle(array $input){...return $this->response()->alert()->success('success')->detail('Details');}
- adjust the form
blocklayout function, and deprecate thesetDefaultBlockWidthmethod, please refer to the document form block layout, Example for detailed Usage
$form->block(8, function (Form\BlockForm $form) {$form->title('Basic Settings');$form->showFooter();$form->width(9, 2);$form->column(6, function (Form\BlockForm $form) {$form->display('id');$form->text('name');$form->email('email');$form->image('avatar');$form->password('password');});$form->column(6, function (Form\BlockForm $form) {$form->text('username');$form->email('mobile');$form->textarea('description');});});$form->block(4, function (Form\BlockForm $form) {$form->title('Sub-block 2');$form->text('nickname');$form->number('age');$form->radio('status')->options(['1' => 'Default', 2 => 'Frozen'])->default(1);$form->next(function (Form\BlockForm $form) {$form->title('Sub-block 3');$form->date('birthday');$form->date('created_at');});});
- Deprecate the direct form submission, keep only the
ajaxsubmission method, and rename thedisableAjaxSubmitmethod toajax
$form->ajax(false);
Deprecate step-by-step form, please use step-by-step form extension instead in new version
mapandlistboxwill be deprecated soon, and the extension center will be moved.
6. Data repository part of the changes
- The naming of the data repository interface has been simplified, the new interface is as follows
interface Repository{/*** Get primary key name.** @return string*/public function getKeyName();/*** Gets the creation time field.** @return string*/public function getCreatedAtColumn();/*** Get the update time field.** @return string*/public function getUpdatedAtColumn();/*** Whether to use soft delete.** @return bool*/public function isSoftDeletes();/*** Get Grid table data.** @param Grid\Model $model** @return \Illuminate\Contracts\Pagination\LengthAwarePaginator|Collection|array*/public function get(Grid\Model $model);/*** Get edit page data.** @param Form $form** @return array|\Illuminate\Contracts\Support\Arrayable*/public function edit(Form $form);/*** Get detail page data.** @param Show $show** @return array|\Illuminate\Contracts\Support\Arrayable*/public function detail(Show $show);/*** Add a new record.** @param Form $form** @return mixed*/public function store(Form $form);/*** Query the row data before the update.** @param Form $form** @return array|\Illuminate\Contracts\Support\Arrayable*/public function updating(Form $form);/*** Update data.** @param Form $form** @return bool*/public function update(Form $form);/*** Delete Data.** @param Form $form* @param array $deletingData** @return mixed*/public function delete(Form $form, array $deletingData);/*** Query the row data before deletion.** @param Form $form** @return array|\Illuminate\Contracts\Support\Arrayable*/public function deleting(Form $form);}
2.EloquentRepository::eloquent() 重命名为 EloquentRepository::model()
7. Section changes
In the new version AdminSection has been removed, please use Dcat\Admin\Admin::SECTION constant instead
use Dcat\Admin\Admin;admin_inject_default_section(Admin::SECTION['HEAD'], function () {return ...;});
8. Extensions
For extension-related changes, please refer to the document extensions
9. Login logic
Login template, if you have customized the login template in the old project, you need to adjust the
JScode in the login templateDcat.ready(function () {// ajax form submission$('#login-form').form({validate: true,});});
Login logic, if the login logic has been rewritten, the response method for the final login success needs to use
sendLoginResponse
10. Other changes
- Resource registration ```php use Dcat\Admin\Admin;
// Registered resource path alias Admin::asset()->alias(‘test’, ‘assets/test’);
Admin::asset()->alias(‘Name’, [ ‘js’ => [ // @test will be evaluated as an alias ‘@test/test.js’, ], ‘css’ => [ ‘@test/test.css’, ], ]);
// Loading Resources Admin::asset()->require(‘@Name’); // Load only js Admin::js(‘@Name’); // Load only css Admin::css(‘@Name’); ```
