定制 abevanation/reversify 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

abevanation/reversify

Composer 安装命令:

composer require abevanation/reversify

包简介

Reverse engineer an existing database schema into Laravel migrations, models, services, and controllers

README 文档

README

Note
Reversify is still in testing. Functionality may be unstable at best. If you would like to assist in getting this project off the ground, feel free to fork and submit pull requests for new features, bug fixes, or improvements.

Packagist Version Downloads

Reversify is a Laravel package designed to reverse engineer your existing database schema to create fully functional migrations, models, controllers that include relationships, traits, attributes, and more. It simplifies the process of reverse-engineering your database into Laravel components, making it ideal for legacy systems or rapid development.

Features

  • Migrations: Generate Laravel migrations from your database schema, including foreign keys and constraints.
  • Models: Create Eloquent models with support for:
    • Traits
    • Fillable, guarded, casts, with, and hidden attributes
    • Relationships (e.g., belongsTo, hasMany, etc.)
    • Lifecycle hooks (creating, updating, etc.)
  • Controllers: Generate CRUD resource controllers for your database tables.
  • Dynamic Configuration: Fully configurable to suit your project's needs.

Installation

  1. Require the package via Composer:

    composer require abevanation/reversify
  2. Publish the configuration file:

    php artisan vendor:publish --tag=reversify-config

    This will create a config/reversify.php file where you can customize the package behavior.

  3. Add the ReversifyBlueprintMacroServiceProvider to your bootstrap/providers.php (Laravel 11) or config/app.php (Laravel 10 or earlier) if it isn't already registered:

    App\Providers\ReversifyBlueprintMacroServiceProvider::class,

Configuration

The configuration file (config/reversify.php) allows you to customize how the package generates migrations, models, and controllers.

Example Configuration

return [
    'ignore_tables' => ['migrations', 'failed_jobs'], // Tables to ignore during generation

    'models' => [
        'traits' => [
            'users' => ['Illuminate\Database\Eloquent\Factories\HasFactory', 'App\Traits\Loggable'],
            'App\Traits\SoftDeletes',
        ],
        'fillable' => [
            'users' => ['name', 'email', 'password'],
        ],
        'guarded' => ['created_by', 'updated_by'],
        'casts' => [
            'created_at' => 'datetime',
            'updated_at' => 'datetime',
        ],
        'with' => [
            'users' => ['profile'],
        ],
        'hidden' => [
            'users' => ['password', '*token'],
        ],
        'relationships' => [
            'users' => [
                'belongsTo' => ['App\Models\Organization', 'organization', 'organization_id', 'id'],
                'hasMany' => ['App\Models\Policy', 'policies', 'id', 'user_id'],
            ],
        ],
        'lifecycle_hooks' => [
            'users' => [
                'creating' => 'function ($model) {
                    $model->created_by = auth()->id();
                    $model->created_at = now();
                }',
                'updating' => 'function ($model) {
                    $model->updated_by = auth()->id();
                }',
            ],
        ],
    ],
];

Commands

Generate Migrations

php artisan reversify:migrations
  • Scans the database schema and generates migration files for each table.
  • A separate migration file is created to add foreign key constraints.

Generate Models

php artisan reversify:models
  • Creates Eloquent models for all tables (except ignored ones).
  • Includes configurable attributes like fillable, guarded, casts, relationships, and lifecycle hooks.

Generate Controllers

php artisan reversify:controllers
  • Generates basic CRUD resource controllers for each table.
  • Controllers are placed in the app/Http/Controllers directory.

Generate All Components

php artisan reversify:generate
  • Runs reversify:migrations, reversify:models, and reversify:controllers sequentially.

Output Examples

Migration File

For a table named users:

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration {
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('email')->unique();
            $table->string('password');
            $table->timestamps();
            $table->softDeletes();
        });
    }

    public function down()
    {
        Schema::dropIfExists('users');
    }
};

Model File

For a table named users:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\SoftDeletes;
use App\Traits\Loggable;

class User extends Model
{
    use HasFactory, SoftDeletes, Loggable;

    protected $fillable = ['name', 'email', 'password'];
    protected $guarded = ['created_by', 'updated_by'];
    protected $casts = ['created_at' => 'datetime', 'updated_at' => 'datetime'];
    protected $with = ['profile'];
    protected $hidden = ['password', '*token'];

    public function organization()
    {
        return $this->belongsTo(App\Models\Organization::class, 'organization_id', 'id');
    }

    protected static function booted()
    {
        static::creating(function ($model) {
            $model->created_by = auth()->id();
            $model->created_at = now();
        });
        static::updating(function ($model) {
            $model->updated_by = auth()->id();
        });
    }
}

Controller File

For a table named users:

<?php

namespace App\Http\Controllers;

use App\Models\User;
use Illuminate\Http\Request;

class UserController extends Controller
{
    public function index()
    {
        return User::all();
    }

    public function store(Request $request)
    {
        $validated = $request->validate([
            // Define validation rules here
        ]);

        $model = User::create($validated);

        return response()->json($model, 201);
    }

    public function show(User $model)
    {
        return $model;
    }

    public function update(Request $request, User $model)
    {
        $validated = $request->validate([
            // Define validation rules here
        ]);

        $model->update($validated);

        return response()->json($model, 200);
    }

    public function destroy(User $model)
    {
        $model->delete();

        return response()->json(null, 204);
    }
}

Contributing

Feel free to fork this repository and submit pull requests for new features or improvements. Please ensure all changes are tested.

License

This package is open-source and licensed under the MIT License.

abevanation/reversify 适用场景与选型建议

abevanation/reversify 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 5 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 01 月 13 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 abevanation/reversify 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 abevanation/reversify 我们能提供哪些服务?
定制开发 / 二次开发

基于 abevanation/reversify 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 5
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 16
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-01-13