Skip to content

示例

这里提供完整的 Lartrix 使用示例,帮助你快速上手。

示例列表

快速预览

最简单的 CRUD

php
class UserController extends CrudController
{
    protected function getModelClass(): string
    {
        return User::class;
    }

    protected function listUi(): array
    {
        return success(
            CrudPage::make('用户管理')
                ->apiPrefix('/users')
                ->columns([
                    ['key' => 'id', 'title' => 'ID'],
                    ['key' => 'name', 'title' => '姓名'],
                    ['key' => 'email', 'title' => '邮箱'],
                ])
                ->build()
        );
    }
}

带搜索和筛选

php
protected function listUi(): array
{
    \$schema = CrudPage::make('文章管理')
        ->apiPrefix('/posts')
        ->columns([
            ['key' => 'id', 'title' => 'ID'],
            ['key' => 'title', 'title' => '标题'],
            ['key' => 'status', 'title' => '状态'],
        ])
        ->search([
            ['标题', 'title', Input::make()],
            ['状态', 'status', Select::make()->options([
                ['label' => '启用', 'value' => 1],
                ['label' => '禁用', 'value' => 0],
            ])],
        ]);

    return success(\$schema->build());
}

带操作按钮

php
protected function listUi(): array
{
    \$schema = CrudPage::make('用户管理')
        ->apiPrefix('/users')
        ->columns([
            ['key' => 'id', 'title' => 'ID'],
            ['key' => 'name', 'title' => '姓名'],
            [
                'key' => 'actions',
                'title' => '操作',
                'slot' => [
                    Space::make()
                        ->children([
                            Button::make('编辑')
                                ->type('primary')
                                ->size('small')
                                ->on('click', SetAction::make('editId', '{{ slotData.row.id }}')),
                            Button::make('删除')
                                ->type('error')
                                ->size('small')
                                ->on('click', [
                                    CallAction::make('\$dialog.warning', [
                                        '确认删除',
                                        '此操作不可恢复',
                                        [
                                            'positiveText' => '确认',
                                            'onPositiveClick' => [
                                                FetchAction::make('/users/{{ slotData.row.id }}')->delete(),
                                            ],
                                        ],
                                    ]),
                                ]),
                        ]),
                ],
            ],
        ]);

    return success(\$schema->build());
}

基于 MIT 许可发布