Skip to content

快速开始

Laravel自带的users表举例,表结构为:

sql
users
    id          - integer
    name        - string
    email       - string
    password    - string
    created_at  - timestamp
    updated_at  - timestamp

对应的数据模型为文件 App\User.php

使用laravel-admin可以通过使用以下几步来快速生成users表的CURD操作页面:

创建控制器

使用下面的命令来创建一个App\Models\User模型对应的控制器

shell
// Mac or Linux
php artisan admin:make UserController --model=App\\Models\\User

// Windows
php artisan admin:make UserController --model=App\Models\User

也可以使用admin:controller命令创建控制器:

shell
php artisan admin:controller --model=App\Models\User

上面的命令会创建控制器文件app/Admin/Controllers/UserController.php.

添加路由

在路由配置文件app/Admin/routes.php里添加一行:

php
Route::resource('users', 'UserController')->names('users');

添加菜单栏入口

打开菜单管理页 http://localhost/auth/menus 添加对应的menu, 然后就能在后台管理页面的左侧边栏看到用户管理页面的链接入口了。

如果要添加外部链接,只要填写完整的url即可,比如https://laravel.com/.

编写CURD页面逻辑

通过admin:make命令创建的控制器app/Admin/Controllers/UserController.php如下:

php
<?php

namespace App\Admin\Controllers;

use App\Models\User;
use Elegant\Utils\Controllers\AdminController;
use Elegant\Utils\Form;
use Elegant\Utils\Table;
use Elegant\Utils\Show;

class UserController extends AdminController
{
    protected $title = 'Users';

    protected $model = User::class;

    protected function table()
    {
        $table = new Table(new $this->model());

        $table->column('id', __('Id'));
        $table->column('name', __('Name'));
        $table->column('email', __('Email'));
        $table->column('password', __('Password'));
        $table->column('created_at', __('Created at'));
        $table->column('updated_at', __('Updated at'));

        return $table;
    }

    protected function detail($id)
    {
        $show = new Show($this->model::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('name', __('Name'));
        $show->field('email', __('Email'));
        $show->field('password', __('Password'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));

        return $show;
    }

    protected function form()
    {
        $form = new Form(new $this->model());

        $form->textarea('name', __('Name'));
        $form->textarea('email', __('Email'));
        $form->textarea('password', __('Password'));

        return $form;
    }
}

$title属性用来设置这个CURD模块的标题,可以将它修改为任何其它的字符串。

table方法对应数据的列表页,参考model-table 文档来实现列表页的相关功能逻辑。

detail方法对应数据的详情页,在列表页操作列的详情显示按钮点击进入,参考model-show 文档来实现详情页的相关功能逻辑。

form方法对应数据的创建编辑页,参考model-form 文档来实现数据创建和编辑页的相关功能逻辑。