appoly/smart-schema

此包的最新版本(0.7.12)没有可用的许可证信息。

Laravel 的表单辅助工具

0.7.12 2019-12-12 11:59 UTC

README

厌倦了重复自己?此包将所有与字段相关的内容集中在一起。

导航

快速使用

添加到 composer.json

"appoly/smart-schema": "^0.6.3",

简介

不需要创建迁移、请求、表单视图和设置可填充字段,我们可以创建一个智能迁移来处理所有这些。

示例迁移

SmartSchema::create('sites', function ($table) {
    $table->increments("id");

    $table->integer("name");

    $table->text("name")
        ->nullable()
        ->required()
        ->fillable()
        ->max(5)
        ->forms([
            'type' => 'text',
            'label' => 'Site name'
        ]);

    $table->timestamps();
});

字段选项

字段类型

标准字段类型可用。

$table->text("name")
$table->integer("user_id")
$table->float("latitude")

等等...

虚拟字段

在某些情况下,我们可能需要在表单中添加不直接对应数据库表的字段。

然后我们可以使用

$table->virtual("slot")->forms(...

验证规则

这些可以在迁移中的字段创建时链接。

示例

$table->text("email")->required()->email();

可用的规则

->unique()
->required()
->email()
->max( val )
->min( val )

可以通过添加自定义验证规则

->addRule( rule )

在控制器中存储对象时,应该使用对象类型调用验证辅助函数

public function store(Request $request) {
    SchemaHelper::validate($request, 'sites');
    
    // Process the request
    // ...
}

模型属性

fillable()

转换

->array()
->datetime()

模型 必须SmartField 特性才能使用 fillable() 或任何属性转换。

class User extends Model
{
    use SmartField;
}

表单

表单辅助工具将根据字段数据生成表单(目前为 Bootstrap 4)。

在迁移中使用 ->forms([... 显示自动生成的表单中的字段

->forms([
    'type' => 'text',
    'label' => 'Site name'
])

渲染基本表单

{!! \Appoly\SmartSchema\SchemaHelper::form('sites', route('sites.store')) !!}

需要一组选项的复选框表单字段,如选择框和单选按钮。

对于以下字段

$table->id("role")
        ->forms([
            'type' => 'select', // or 'type' => 'radio'
            'label' => 'Role'
        ]);

选项可以如此传递

{!! \Appoly\SmartSchema\SchemaHelper::form('sites', route('sites.store'), [
    'select_options' => [
        'role_id' => ['User', 'Admin']
    ]
]) !!}

或者使用键

{!! \Appoly\SmartSchema\SchemaHelper::form('sites', route('sites.store'), [
    'select_options' => [
        'role_id' => [
            10001 => 'User', 
            20001 => 'Admin'
        ]
    ]
]) !!}

代码生成

此包包括一个控制台命令,可以设置模板控制器和视图代码。

php artisan crud:generate {resource_name_singular}

例如

php artisan crud:generate client