Quick start

In daily development, we can use the code generator to generate a key to add, delete and check the page code, very convenient and quick.

The following will give you an introduction to the use of the code generator, as well as the basic composition of an add, delete, redact and check the page. By learning the following content will help you quickly understand the basic use of this system.

code generator

Create a data table

A migration file for the users table is built in after installing Laravel (if you don’t know what the migration file does, refer to the document [Database Migration] (https://learnku.com/docs/laravel/7.x/migrations/7496)). The file path is database/migrations/2014_10_12_000000_create_users_table.php.

Then we run the following command to create this table in MySQL

  1. php artisan migrate

After running it, you can see that there is already an extra users table in the database, which is structured as follows

  1. CREATE TABLE `users` (
  2. `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  3. `name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  4. `email` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  5. `password` varchar(60) COLLATE utf8_unicode_ci NOT NULL,
  6. `remember_token` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
  7. `created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  8. `updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  9. PRIMARY KEY (`id`),
  10. UNIQUE KEY `users_email_unique` (`email`)
  11. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci

Generate add, delete and check pages with one click

{tip} If your development environment is not windows, please note that to set read and write permissions to the project directory, otherwise there may be unable to generate code.

1. First, open the address http://你的域名/admin/helpers/scaffold to the code generator page.

2.As the data table has already been created, we can directly select the users table from the second drop-down box in the upper left corner of the page, and the fields will be automatically filled with information after selection.

Quick start - 图1

3.Modify the model name to App\User.

4. Since the migration file, the datasheet, and the model file (just use the built-in App\User) already exist, we can remove these three options here.

5.Fill-in field translation

Finally, the result is presented as follows

Quick start - 图2

Finally, click the Create button to create the following file

  1. app/Admin
  2. ├── Controllers
  3. └── UserController.php # controller
  4. └── Repositories # repository
  5. └── User.php
  6. resouces/lang/{当前语言}
  7. └── user.php # language pack

Add route configuration

Open the routing configuration file app/Admin/routes.php and add a line to it:

  1. $router->resource('users', 'UserController');

At this point, you can open your browser and enter the address http://YourDomain/admin/users to access the page you just created!

Adding the left menu

Open http://YourDomain/admin/auth/menu and add the corresponding menu, then you can see the link to the user management page in the left sidebar of the backend administration page.

Where uri fill in the part of the path that does not contain the route prefix, such as the full path is http://YourDomain/admin/demo/users, then fill in demo/users, if you want to add external links, just fill in the full url, such as http://dcat-admin.org/.

Menu translation

Append the menu TITLE to the menu_titles index of your language file. For example, for the “Workplace” TITLE:

In resources/lang/{current language}/menu.php

  1. ...
  2. // Replace spaces with _ lowercase and _
  3. 'titles' => [
  4. 'work_units' => 'Unidades de trabajo'
  5. ],

Done

Such a simple CURD function on the build is complete, the rest of the work is the depth of building data tables and forms, open app/Admin/Contollers/UserController.php, find the form() and grid() methods, then add the build code. For more detailed usage, please see data form and data form.

A brief description of the add/drop/remove/check function

To make it easier to understand the basic Usages of the Add/Delete function, the following will give you a brief overview of the code generated earlier using the generator.

Controller

The add/remove/check page code of Dcat Admin is very simple and easy to understand, very developer friendly, requires very little code to build a fully functional backend system, and is very simple, flexible and easy to extend.

Open app/Admin/Controllers/UserController.php can see the following code

  1. <?php
  2. namespace App\Admin\Controllers;
  3. use App\Admin\Repositories\User;
  4. use Dcat\Admin\Form;
  5. use Dcat\Admin\Grid;
  6. use Dcat\Admin\Show;
  7. use Dcat\Admin\Http\Controllers\AdminController;
  8. class UserController extends AdminController
  9. {
  10. /**
  11. * Make a grid builder.
  12. *
  13. * @return Grid
  14. */
  15. protected function grid()
  16. {
  17. return Grid::make(new User(), function (Grid $grid) {
  18. // The fields here will automatically use translated files
  19. $grid->column('id')->sortable();
  20. $grid->column('name');
  21. $grid->column('email');
  22. $grid->column('email_verified_at');
  23. $grid->column('password');
  24. $grid->column('remember_token');
  25. $grid->column('created_at');
  26. $grid->column('updated_at')->sortable();
  27. $grid->filter(function (Grid\Filter $filter) {
  28. $filter->equal('id');
  29. });
  30. });
  31. }
  32. /**
  33. * Make a show builder.
  34. *
  35. * @param mixed $id
  36. *
  37. * @return Show
  38. */
  39. protected function detail($id)
  40. {
  41. return Show::make($id, new User(), function (Show $show) {
  42. // The fields here will automatically use translated files
  43. $show->field('id');
  44. $show->field('name');
  45. $show->field('email');
  46. $show->field('email_verified_at');
  47. $show->field('password');
  48. $show->field('remember_token');
  49. $show->field('created_at');
  50. $show->field('updated_at');
  51. });
  52. }
  53. /**
  54. * Make a form builder.
  55. *
  56. * @return Form
  57. */
  58. protected function form()
  59. {
  60. return Form::make(new User(), function (Form $form) {
  61. // The fields here will automatically use translated files
  62. $form->display('id');
  63. $form->text('name');
  64. $form->text('email');
  65. $form->text('email_verified_at');
  66. $form->text('password');
  67. $form->text('remember_token');
  68. $form->display('created_at');
  69. $form->display('updated_at');
  70. });
  71. }
  72. }

Repositories

Dcat Admin building pages do not directly rely on Model, but the introduction of the data warehouse as an intermediate layer, so that the construction of the page is no longer with the read and write data to produce a strong coupling.

Data repository is the specific implementation of the Dcat Admin interface for data add/drop operations, please refer to data repository for more details about Usage.

If your data comes from MySQL, then you can also use Model instance directly. To make it easier for you to understand the concept, we’ve created a data warehouse file here.

We open the just generated file app/Admin/Repositories/User.php, we can see only the following contents, which is quite simple

  1. <?php
  2. namespace App\Admin\Repositories;
  3. use Dcat\Admin\Repositories\EloquentRepository;
  4. use App\User as UserModel;
  5. class User extends EloquentRepository
  6. {
  7. protected $eloquentClass = UserModel::class;
  8. }

Language packs

Each controller can generate its own language pack, and the data-grid, data-form and data-detail functions will automatically read the translations.

Let’s open the UserController language package file resouces/lang/{current language}/user.php.

  1. <?php
  2. return [
  3. // labels is a custom label translation
  4. 'labels' => [
  5. // This is a translation of the page title
  6. 'User' => '用户',
  7. ],
  8. // Table field translation
  9. 'fields' => [
  10. 'name' => '名称',
  11. 'email' => '邮箱',
  12. 'email_verified_at' => '验证时间',
  13. 'password' => '密码',
  14. 'remember_token' => 'remember_token',
  15. ],
  16. 'options' => [
  17. ],
  18. ];