rapid/laplus
Composer 安装命令:
composer require rapid/laplus
包简介
Laravel Plus+
README 文档
README
Laravel plus+ add presentation for your models
public function present() { $this->id(); $this->text('title'); $this->string('password')->cast('hashed')->hidden(); $this->belongsTo(Artist::class)->cascadeOnDelete(); }
What's Changed In V4
- Command names changed
- New development & production deploy tools
- Removed slug and file columns
- Supports renames and changes a column at the same time
- Supports package resources
- Added travels (read more...)
- Added validation generators (read more...)
Features
1. Migration Generating
Define the structure of your model: columns, indexes, data types, enums, relationships, or even extensibility with traits.
class Blog extends Model { use HasPresent; use HasSlug; // Will extend the 'slug' column public function present(Present $present) { $present->id(); $present->string('title'); $present->belongsTo(Category::class); $present->enum('status', Status::class); $present->yield(); $present->timestamps(); } }
Don't get involved in migrations! Because Laplus has taken responsibility for all migrations, and it builds all migrations by reading your presentations 😎
2. Auto Fills
No need to redefine $fillable, $cast and $hidden! Defining it in the present specifies everything. So Laplus will autofill these variables. 😉
// These values will automatically fills: // protected $fillable = ['id', 'slug']; // protected $cast = ['password' => 'hashed']; // protected $hidden = ['password'];
3. IDE Friendly
When all the columns, their types, and their relationships are known, why shouldn't the IDE show them to us?
Laplus introduces all of this to the IDE by generating a neat (and out-of-model) document. 📝
/** * @property int $id * @property string $name * @property \App\Enums\Gender $gender * @method BelongsTo<Profile> profile() * @property Profile $profile */
4. Travels
Once a version of the project has been deployed, it is almost impossible to change the database.
Travels allow you to make changes to the table records as well as change the table structure. ✈️
return new class extends Travel { public string|array $on = User::class; public string|array $whenAdded = 'full_name'; public string|array $prepareNullable = 'full_name'; public function up(): void { User::all()->each(function (User $user) { $user->update([ 'full_name' => $user->first_name . ' ' . $user->last_name, ]); }); } };
In the above example, we are going to remove first_name and last_name from the users table
and replace them with full_name.
The above class is responsible for generating the full_name value using the previous
data before deleting first_name and last_name.
Requirements
- Php 8.2 or higher
- Laravel 11.0
Documents
- Installation
- Configuration
- Present
- Dev Utils
- Deploy
- Label
- Guide
- Travel
- Package Development
- Validation Generator
Installation
1- Install the package with composer:
composer require rapid/laplus
2- Publish configs (Optional)
Run this command to publish configs to config/laplus.php
php artisan vendor:publish --tag=laplus
3- Convert default User model to presentable model (Optional):
- Add
HasPresenttrait:
class User extends Model { use HasPresent; }
- Remove
$fillable,$hiddenandcasts()values:
//protected $fillable = ['name', 'email', 'password']; //protected $hidden = ['password', 'remember_token']; //protected function casts() { ... }
Laplus will automatically add this values.
Add below code in User class:
protected function present(Present $present) { $present->id(); $present->string('name'); $present->string('email')->unique(); $present->timestamp('email_verified_at')->nullable(); $present->password(); $present->rememberToken(); $present->timestamps(); }
- Move default migration to laplus path:
Find database/migrations/0001_01_01_000000_create_users_table.php file and move it
into database/migrations/deploy folder (create it if doesn't exists)
Development Utils
To be able to update your tables during development, you can use the following command:
php artisan dev:migrate --guide
Deployment
When deploying the project to production, you can create the final migrations with the following command:
php artisan deploy:migrate
For greater project safety, you can follow the scenario we have written on the page below.
Make model
You can use this command to create a model and a present:
php artisan make:model-laplus Name
This command will create app/Models/Name.php with inline presentation.
Make model with separately present
You can use this command to create a model with present class:
php artisan make:model-laplus --present Name
This command will create app/Models/Name.php model and app/Presents/NamePresent.php present.
Migrations
Generate Migrations
Run this command to automatically found the updates and generate migrations:
php artisan dev:migration
Update Database
Following command, run dev:migration and migrate at once:
php artisan dev:migrate
Label Translator
Present the model labels:
class UserLabelTranslator extends LabelTranslator { public function gender(bool $emoji = false) { return $this->value?->toString($emoji); // Returns gender name or null } }
And use it easily:
<p>{{ $user->gender_label }}</p> <p>{{ $user->gender_label(emoji: true) }}</p>
Labels always return a string value. If the value is null, it returns
"Undefined".
Guide Generator
Guide automatically generate the model docblock using the columns, attributes and relationships:
class User extends Model { use HasPresent; use HasLabels; protected function present(Present $present) { $present->id(); $present->string('name'); } #[IsRelation] public function avatars() { return $this->hasMany(Avatar::class); } public function getFirstName() : string { return Str::before($this->name, ' '); } }
It generates:
/** * @Guide * @property int $id * @property string $name * @property Collection<int, Avatar> $avatars * @property string $name_label * @property string name_label() * @property string $first_name * @EndGuide */ class User extends Model
More Document
More document found in Documents section.
rapid/laplus 适用场景与选型建议
rapid/laplus 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 740 次下载、GitHub Stars 达 21, 最近一次更新时间为 2024 年 04 月 05 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 rapid/laplus 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 rapid/laplus 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 740
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 21
- 点击次数: 6
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-04-05