chargefield/laravel-savable
Composer 安装命令:
composer require chargefield/laravel-savable
包简介
Savable is a Laravel package that will help you organize your business logic.
关键字:
README 文档
README
Laravel Savable
Savable is a Laravel package that will help you organize your business logic.
Installation
You can install the package via composer:
composer require chargefield/laravel-savable
Usage
Savable Trait
namespace App\Models; use Illuminate\Database\Eloquent\Model; use Chargefield\Savable\Traits\IsSavable; class Post extends Model { use IsSavable; }
Example
A simple example for storing a record from a controller:
namespace App\Http\Controllers; use App\Models\Post; use Chargefield\Savable\Fields\SlugField; use Chargefield\Savable\Fields\StringField; use Illuminate\Http\Request; class PostController { public function store(Request $request) { $post = Post::make()->savable($request->all())->columns([ StringField::make('title'), SlugField::make('slug')->fromField('title'), StringField::make('body'), ])->save(); } }
Savable Columns
Setting columns:
$post = Post::make()->savable([...])->columns([ StringField::make('title'), SlugField::make('slug')->fromField('title'), StringField::make('body'), ])->save();
Alternatively, you can set savable columns in a model:
namespace App\Models; use Illuminate\Database\Eloquent\Model; use Chargefield\Savable\Fields\Field; use Chargefield\Savable\Traits\IsSavable; use Chargefield\Savable\Fields\JsonField; use Chargefield\Savable\Fields\SlugField; use Chargefield\Savable\Fields\FileField; use Chargefield\Savable\Fields\StringField; use Chargefield\Savable\Fields\BooleanField; use Chargefield\Savable\Fields\IntegerField; use Chargefield\Savable\Fields\DatetimeField; class Post extends Model { use IsSavable; /** * @return Field[] */ public function savableColumns(): array { return [ StringField::make('title')->rules('required|string'), SlugField::make('slug')->fromField('title'), StringField::make('body')->rules('required|string'), FileField::make('image')->nullable()->rules('nullable|image'), BooleanField::make('is_featured')->rules('required|boolean'), IntegerField::make('order')->strict()->rules('required|integer|min:1'), JsonField::make('options')->nullable(), DatetimeField::make('published_at')->nullable(), ]; } }
NOTE: savableColumns() will get overridden by columns([...])
Savable Data
Setting data:
$post = Post::make()->savable(request()->all())->columns([...])->save();
or
$post = Post::make()->savable()->data(request()->all())->columns([...])->save();
Setting data from request:
$post = Post::make()->savable()->fromRequest()->columns([...])->save();
Setting data from a given request:
$post = Post::make()->savable()->fromRequest(request())->columns([...])->save();
Validation
Validating before saving (throws Illuminate\Validation\ValidationException):
$post = Post::make()->savable()->data([...])->columns([...])->validate()->save();
Validating without throwing an exception:
Post::make()->savable()->data([...])->columns([...])->hasErrors(); // return bool
or
Post::make()->savable()->data([...])->columns([...])->getErrors(); // return Illuminate\Support\MessageBag
NOTE: Fields must set rules([...]) in order to validate their data.
Fields
String Field:
StringField::make('title');
Slug Field:
SlugField::make('slug')->fromField('title')->separateBy('-');
File Field:
FileField::make('image')->disk('local')->path('images')->withOriginalName();
Boolean Field:
BooleanField::make('is_featured');
Integer Field:
IntegerField::make('age')->strict();
Json Field:
JsonField::make('options')->pretty()->depth(512);
Datetime Field:
DatetimeField::make('published_at');
Additional Methods:
Sets the column name and default value:
StringField::make('title', 'Default Title');
or
StringField::make('title')->value('Default Title');
Sets the field name if not the same as the column name:
StringField::make('title')->fieldName('name');
Sets the nullable flag, null will be returned if value is empty/null/exception:
StringField::make('title')->nullable();
Sets the validation rules for the field (Laravel validation rules):
StringField::make('user_id')->rules('required|exists:users,id');
or
StringField::make('user_id')->rules([ 'required', Rule::exists('users', 'id'), ]);
Sets a closure to transform the value:
StringField::make('title')->transform(function ($fieldName, $fieldValue, $fieldsData) { return Str::title($fieldValue); });
Custom Fields
You can create custom fields with ease using the artisan command.
php artisan make:field CustomField
Outputs:
namespace App\Fields; use Chargefield\Savable\Fields\Field; class CustomField extends Field { /** * @param array $data * @return mixed */ public function handle(array $data = []) { if (empty($this->value) && $this->nullable) { return null; } // Logic goes here return $this->value; } }
Testing custom fields:
Field::assertHandle
$field = CustomField::fake('title'); $field->value('Example Title'); $field->assertHandle('Example Title'); // passed $field->assertHandle('Not The Same'); // failed
Field::assertTransform
$field = CustomField::fake('title'); $field->value('Example Title'); $field->transform(function ($name, $value, $data) { return "{$data['prefix']} {$value}"; }); $field->assertTransform('Prefixed Example Title', ['prefix' => 'Prefixed']); // passed $field->assertTransform('Example Title', ['prefix' => 'Prefixed']); // failed
Field::assertValidation
$field = CustomField::fake('title'); $field->rules('required|string'); $field->assertValidation('Example Text'); // passed $field->assertValidation(''); // failed
Testing
You can run the tests with:
vendor/bin/phpunit
Changelog
Please see CHANGELOG for more information what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email support@chargefield.com instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.
chargefield/laravel-savable 适用场景与选型建议
chargefield/laravel-savable 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 5 次下载、GitHub Stars 达 0, 最近一次更新时间为 2021 年 09 月 05 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「chargefield」 「savable」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 chargefield/laravel-savable 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 chargefield/laravel-savable 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 chargefield/laravel-savable 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Alfabank REST API integration
Laravel package for Accurate Online API integration.
Shared RCX Laravel DataTables UI and configuration helpers.
Boot a Laravel project on any machine with one command: app:serve installs missing tools (PHP, Node, Composer, Herd, Docker), creates .env, sets up the database, runs migrations, builds assets, starts a queue worker and serves via Herd, Sail or artisan serve; app:down cleanly stops everything it sta
Branded, diagnostic error pages (500, 403, 404, 419, 503) for Filament — native Filament UI, dark mode and translations out of the box.
Turn any PDF into a Pingen-ready A4 letter (generated address cover page + A4 normalisation + safe margins) and send it through the Pingen print & mail API. Laravel-first.
统计信息
- 总下载量: 5
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 2
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-09-05
