vleroy / smart
Laravel Smart
v1.0.1
2020-08-10 12:16 UTC
Requires
- doctrine/dbal: ^2.8
- illuminate/console: ^7.0
- illuminate/support: ^7.0
- illuminate/validation: ^7.0
Requires (Dev)
- orchestra/testbench: ^3.8@dev
- phpunit/phpunit: ^7.4@dev
This package is auto-updated.
Last update: 2024-09-10 21:04:56 UTC
README
请不要在生产环境中使用此包!
- 这是一个概念验证。(更新:此包已成功应用于拥有25个模型的app中)
- 代码没有进行单元测试。(更新:感谢@robsontenorio,我们已经有了基本的单元测试)
- API不稳定。
我发布这个alpha版本是为了获取对API的反馈,并确保我没有错过重要的用例。
此外,我无法独自完成这个项目!我非常愿意接受社区的援助。不要害羞!
- 加入讨论(即使不是你的问题)
- 改进文档(如果你发现某些内容难以理解,请提出问题,发送pull-request等)
- 贡献代码(我会尽可能引导你)
想法
如果你在模型中定义了字段,你可以获得
- 自动迁移
- 在
save()
上的自动验证 - 分组字段定义(名称、模式类型、转换、可填充/受保护、验证规则、模式修改器、模式索引)
<?php namespace App; use Deiucanta\Smart\Field; use Deiucanta\Smart\Model; class Product extends Model { public function fields() { return [ Field::make('id')->increments(), Field::make('sku')->string()->fillable()->required()->unique()->label("Sku Number"), Field::make('name')->string()->fillable()->required(), Field::make('price')->decimal(6, 2)->fillable()->required()->min(0), Field::make('description')->text()->fillable()->nullable(), Field::make('created_at')->timestamp()->nullable()->index(), Field::make('updated_at')->timestamp()->nullable()->index(), ]; } }
安装
要安装此包,只需运行
composer require deiucanta/smart
用法
创建一个Smart模型
php artisan smart:model Product
在配置文件(config/smart.php
)中包含模型
注意:现在在创建模型时将自动完成此操作。
<?php return [ 'models' => [ \App\Product::class, ] ];
创建一个Smart迁移
php artisan smart:migration
你应该能在迁移目录中看到一个新迁移。要应用迁移,你只需像往常一样运行php artisan migrate
。
你将在应用数据库的database
目录中找到一个名为smart.json
的新文件。这就是包存储数据库/模型当前状态的地方。如果你删除该文件并运行php artisan smart:migration
,它将创建一个新迁移,就像模式为空一样。
API
这些是你应该能够创建任何类型字段的原始方法。然而,还有一些更智能的方法可以使用。
基本方法
类型方法
这些是当前支持的Laravel类型。
这里有一些自定义类型,仅作为一个示例。