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.
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.
belongsTo, hasMany, etc.)creating, updating, etc.)Require the package via Composer:
composer require abevanation/reversify
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.
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,
The configuration file (config/reversify.php) allows you to customize how the package generates migrations, models, and controllers.
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();
}',
],
],
],
];
php artisan reversify:migrations
php artisan reversify:models
fillable, guarded, casts, relationships, and lifecycle hooks.php artisan reversify:controllers
app/Http/Controllers directory.php artisan reversify:generate
reversify:migrations, reversify:models, and reversify:controllers sequentially.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');
}
};
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();
});
}
}
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);
}
}
Feel free to fork this repository and submit pull requests for new features or improvements. Please ensure all changes are tested.
This package is open-source and licensed under the MIT License.
How can I help you explore Laravel packages today?