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

  1. composer remove dcat/laravel-admin

Re-installation

  1. composer require dcat/laravel-admin:"2.*"

After installing success

  1. delete the public/vendors directory
  2. republish the resource php artisan admin:publish --force.
  3. write the modified parameters to the new configuration file config/admin.php according to the above backed up configuration file, here it should be noted that the default theme color for 1.x is indigo (deprecated), which has been replaced with default in the new version
  4. Adjust the language pack, the language pack directory is changed from zh-CN to zh_CN in the new version, you need to move the custom translation files to the new directory, and the translation of menuTITLE is also separated into menus.php.
  5. Run the database migration command php artisan migrate, two new tables admin_settings and admin_extensions have been added in the new version

3. Global change namespace

  1. Global search namespace Dcat\Admin\Controllers and replace it with Dcat\Admin\Http\Controllers
  2. global search namespace Dcat\Admin\Auth and replace it with `Dcat\Admin\Http\Auth

4. Form section changes

  1. The field hiding function has been adjusted, the responsive method has been deprecated in the old version, in the new version the field hiding function is enabled as follows
  1. // Turn on the field selector function
  2. $grid->showColumnSelector();
  3. // Set default hidden fields
  4. $grid->hideColumns(['field1', ...]) ;
  1. The methods collection and fetching have been removed from the table and can be replaced in the new version by the following events
  1. use Dcat\Admin\Grid;
  2. use Illuminate\Support\Collection;
  3. // Use Grid\Events\Fetched event instead of collection
  4. $grid->listen(Grid\Events\Fetched::class, function ($grid, Collection $rows) {
  5. $rows->transform(function ($row) {
  6. // Changing row data
  7. $row['name'] = $row['first_name'].' '.$row['last_name'];
  8. return $row;
  9. });
  10. });
  11. // Use Grid\Events\Fetching event instead of fetching
  12. $grid->listen(Grid\Events\Fetching::class, function ($grid) {
  13. });
  1. The use of models is allowed in the table row closure function
  1. $grid->column('avatar')->display(function ({
  2. // Direct access to model-related methods
  3. return $this->getAvatar();
  4. });
  1. Set route prefix method from resource to setResource.

    1. $grid->setResource('auth/users');
  2. Tree form tree method will be deprecated soon and will be moved to extension center

5. Changes to the form section

  1. Adjusted form handling response methods, success, error, redirect and location methods have been removed from the old version. In 2.0 we unified the form response methods with the action response methods, please refer to the document form response, Example for detailed Usage
  1. $form->saving(function (Form $form) {
  2. return $form
  3. ->response()
  4. ->success('Save success')
  5. ->script('console.log("Execution of JS code")')
  6. ->redirect('auth/users');
  7. });

In the case of tools-form, the Usage is as follows

  1. public function handle(array $input)
  2. {
  3. ...
  4. return $this
  5. ->response()
  6. ->alert()
  7. ->success('success')
  8. ->detail('Details');
  9. }
  1. adjust the form block layout function, and deprecate the setDefaultBlockWidth method, please refer to the document form block layout, Example for detailed Usage
  1. $form->block(8, function (Form\BlockForm $form) {
  2. $form->title('Basic Settings');
  3. $form->showFooter();
  4. $form->width(9, 2);
  5. $form->column(6, function (Form\BlockForm $form) {
  6. $form->display('id');
  7. $form->text('name');
  8. $form->email('email');
  9. $form->image('avatar');
  10. $form->password('password');
  11. });
  12. $form->column(6, function (Form\BlockForm $form) {
  13. $form->text('username');
  14. $form->email('mobile');
  15. $form->textarea('description');
  16. });
  17. });
  18. $form->block(4, function (Form\BlockForm $form) {
  19. $form->title('Sub-block 2');
  20. $form->text('nickname');
  21. $form->number('age');
  22. $form->radio('status')->options(['1' => 'Default', 2 => 'Frozen'])->default(1);
  23. $form->next(function (Form\BlockForm $form) {
  24. $form->title('Sub-block 3');
  25. $form->date('birthday');
  26. $form->date('created_at');
  27. });
  28. });
  1. Deprecate the direct form submission, keep only the ajax submission method, and rename the disableAjaxSubmit method to ajax
  1. $form->ajax(false);
  1. Deprecate step-by-step form, please use step-by-step form extension instead in new version

  2. map and listbox will be deprecated soon, and the extension center will be moved.

6. Data repository part of the changes

  1. The naming of the data repository interface has been simplified, the new interface is as follows
  1. interface Repository
  2. {
  3. /**
  4. * Get primary key name.
  5. *
  6. * @return string
  7. */
  8. public function getKeyName();
  9. /**
  10. * Gets the creation time field.
  11. *
  12. * @return string
  13. */
  14. public function getCreatedAtColumn();
  15. /**
  16. * Get the update time field.
  17. *
  18. * @return string
  19. */
  20. public function getUpdatedAtColumn();
  21. /**
  22. * Whether to use soft delete.
  23. *
  24. * @return bool
  25. */
  26. public function isSoftDeletes();
  27. /**
  28. * Get Grid table data.
  29. *
  30. * @param Grid\Model $model
  31. *
  32. * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator|Collection|array
  33. */
  34. public function get(Grid\Model $model);
  35. /**
  36. * Get edit page data.
  37. *
  38. * @param Form $form
  39. *
  40. * @return array|\Illuminate\Contracts\Support\Arrayable
  41. */
  42. public function edit(Form $form);
  43. /**
  44. * Get detail page data.
  45. *
  46. * @param Show $show
  47. *
  48. * @return array|\Illuminate\Contracts\Support\Arrayable
  49. */
  50. public function detail(Show $show);
  51. /**
  52. * Add a new record.
  53. *
  54. * @param Form $form
  55. *
  56. * @return mixed
  57. */
  58. public function store(Form $form);
  59. /**
  60. * Query the row data before the update.
  61. *
  62. * @param Form $form
  63. *
  64. * @return array|\Illuminate\Contracts\Support\Arrayable
  65. */
  66. public function updating(Form $form);
  67. /**
  68. * Update data.
  69. *
  70. * @param Form $form
  71. *
  72. * @return bool
  73. */
  74. public function update(Form $form);
  75. /**
  76. * Delete Data.
  77. *
  78. * @param Form $form
  79. * @param array $deletingData
  80. *
  81. * @return mixed
  82. */
  83. public function delete(Form $form, array $deletingData);
  84. /**
  85. * Query the row data before deletion.
  86. *
  87. * @param Form $form
  88. *
  89. * @return array|\Illuminate\Contracts\Support\Arrayable
  90. */
  91. public function deleting(Form $form);
  92. }

2.EloquentRepository::eloquent() 重命名为 EloquentRepository::model()

7. Section changes

In the new version AdminSection has been removed, please use Dcat\Admin\Admin::SECTION constant instead

  1. use Dcat\Admin\Admin;
  2. admin_inject_default_section(Admin::SECTION['HEAD'], function () {
  3. return ...;
  4. });

8. Extensions

For extension-related changes, please refer to the document extensions

9. Login logic

  1. Login template, if you have customized the login template in the old project, you need to adjust the JS code in the login template

    1. Dcat.ready(function () {
    2. // ajax form submission
    3. $('#login-form').form({
    4. validate: true,
    5. });
    6. });
  2. Login logic, if the login logic has been rewritten, the response method for the final login success needs to use sendLoginResponse

10. Other changes

  1. 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’); ```