kejojedi/crudify

Laravel 7 CRUD 应用程序脚手架和生成器。

1.3.11 2020-04-06 13:51 UTC

This package is auto-updated.

Last update: 2024-09-06 23:51:52 UTC


README

Imgur

Crudify

Crudify 是一个 Laravel 7 包,它包含合理的 CRUD 应用程序脚手架和一个生成器,使您的生活更加轻松。它通过 crudify:install 命令自动化初始 CRUD 应用程序设置,并通过 crudify:generate 命令为您生成 CRUD 资源文件。它还包括表单组件,使创建表单变得轻而易举。

它已配置为与 PHPStorm、Valet 和 Laragon 等程序很好地工作。此包需要安装 Node.js 以运行 npm 命令。

有用链接

安装

安装 Laravel

laravel new app

配置 .env 文件

APP_NAME=App
APP_URL=http://app.test
DB_DATABASE=app
MAIL_USERNAME=mailtrap_username
MAIL_PASSWORD=mailtrap_password
MAIL_FROM_ADDRESS=info@app.test

需要 Crudify

composer require kejojedi/crudify

安装 Crudify

php artisan crudify:install

访问您的应用程序 URL 并使用

Email: admin@example.com
Password: password

可以在任何时间从您的 DatabaseSeeder 中删除对 AdminUserSeeder 的调用。

生成 CRUD

为新的模型运行 crudify:generate

php artisan crudify:generate Model

这将生成

  • 控制器
  • 数据表
  • 表单请求
  • 模型
  • 工厂
  • 迁移
  • 播种器
  • 视图文件
  • 导航栏链接
  • 路由

更新新迁移文件后,别忘了迁移。

提示:使用 --force 来替换现有生成的文件,例如 php artisan crudify:generate Model --force

数据表

Crudify 包含了对 yajra/laravel-datatables-html 的包装,以便构建数据表时更加简洁和声明式。生成的模型数据表类位于 app\Http\Datatables

声明

protected function columns()
{
    return [
        Column::make('id'),
        Column::make('name'),
        Column::make('created_at'),
        Column::make('updated_at'),
    ];
}

定义默认排序顺序的不同方法

protected $order_by = 'id'; // sorts by id, ascending
protected $order_by = ['created_at', 'desc']; // sorts by created_at, descending

protected function orderBy()
{
    return 'id'; // sorts by id, ascending
}

protected function orderBy()
{
    return ['created_at', 'desc']; // sorts by created_at, descending
}

注意:每个用户在浏览器中无限期保存每张表的每页条目和排序首选项,因此这将仅设置初始默认顺序。

向数据表 HTML 构建器添加方法的示例

protected function htmlMethods(Builder &$html)
{
    $html->ajax([
        'url' => route('users.index'),
        'type' => 'GET',
        'data' => 'function(d) { d.key = "value"; }',
    ]);
}

向数据表 JSON 构建器添加方法的示例

protected function jsonMethods(DataTableAbstract &$datatables)
{
    $datatables->editColumn('name', function(User $user) {
        return 'Hi ' . $user->name . '!';
    });
}

提示:如果您不想数据表包含操作列,只需完全删除 actions() 方法即可。

表单组件

Crudify 提供简单的表单组件,使构建表单快速且简单。以下为每个组件的最小和完整示例。

输入

<x-crudify-input name="name" :value="old('name', $car->name ?? '')" />
<x-crudify-input name="year" type="number" :label="__('Year')" id="car_year" :value="old('year', $car->year ?? '')" />

文本区域

<x-crudify-textarea name="description" :value="old('description', $car->description ?? '')" />
<x-crudify-textarea name="description" rows="5" :label="__('Car Description')" id="car_description" :value="old('description', $car->description ?? '')" />

选择

<x-crudify-select name="fuel_type" :options="['Gas', 'Diesel', 'Electric']" :value="old('fuel_type', $car->fuel_type ?? '')" />
<x-crudify-select name="fuel_type" :options="['Gas', 'Diesel', 'Electric']" :empty="false" :label="__('Car Fuel Type')" id="car_fuel_type" :value="old('fuel_type', $car->fuel_type ?? '')" />

注意:如果选项是关联数组,则键用作标签,值用作值。对于顺序数组,值用于标签和值。

文件

<x-crudify-file name="image" />
<x-crudify-file name="other_images" :label="__('Other Car Images')" :file-label="__('Choose Images')" id="other_car_images" :multiple="true" />

复选框

<x-crudify-checkbox name="insured" :value="old('insured', $car->insured ?? 0)" />
<x-crudify-checkbox name="insured" :label="__('Insured')" :checkbox-label="__('This car is insured')" id="car_insured" :value="old('insured', $car->insured ?? 0)" />

注意:复选框属性应具有 boolean 迁移列。

复选框组

<x-crudify-checkboxes name="features" :options="['Bluetooth', 'Navigation', 'Speakers']" :value="old('features', $car->features ?? [])" />
<x-crudify-checkboxes name="features" :options="['Bluetooth', 'Navigation', 'Speakers']" :label="__('Car Features')" id="car_features" :value="old('features', $car->features ?? [])" />

注意:复选框组属性应转换为 array 并具有 text 迁移列。

单选按钮

<x-crudify-radios name="color" :options="['Red' => '#ff0000', 'Green' => '#00ff00', 'Blue' => '#0000ff']" :value="old('color', $car->color ?? '')" />
<x-crudify-radios name="color" :options="['Red' => '#ff0000', 'Green' => '#00ff00', 'Blue' => '#0000ff']" :label="__('Car Color')" id="car_color" :value="old('color', $car->color ?? '')" />

提示:您可以通过检查 isset($model)(例如 isset($car))来确定字段是否显示在 createedit 页面上。如果设置了 $model,则表示用户正在编辑页面上。

使用的包

Composer 包

NPM 软件包