承接 cxuan1225/laravel-api-from-table 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

cxuan1225/laravel-api-from-table

最新稳定版本:v0.3.0

Composer 安装命令:

composer require cxuan1225/laravel-api-from-table

包简介

Generate Laravel API classes from existing database tables.

README 文档

README

Generate Laravel API classes from existing database tables.

Laravel API From Table is a database-first code generator for Laravel projects. It reads an existing table and generates the API layer around it: model, form requests, DTOs, actions, resource, and controller.

Why This Package Exists

Laravel already provides commands such as:

php artisan make:model Customer -crR

That command is useful for generating empty skeleton files.

However, it does not inspect your existing database table columns, nullable fields, defaults, indexes, or column types.

This package reads your database table schema and generates more useful Laravel code based on the actual database structure.

Generated Files

Running the command for a customers table can generate:

app/Models/Customer.php
app/Http/Requests/StoreCustomerRequest.php
app/Http/Requests/UpdateCustomerRequest.php
app/Data/StoreCustomerData.php
app/Data/UpdateCustomerData.php
app/Actions/Customers/StoreCustomerAction.php
app/Actions/Customers/UpdateCustomerAction.php
app/Http/Resources/CustomerResource.php
app/Http/Controllers/CustomerController.php

Generated API flow:

FormRequest
→ DTO
→ Action
→ Eloquent Model
→ JsonResource
→ API Controller

Core Idea

Database Table
→ Schema Reader
→ TableSchema DTO
→ Inferrers
→ Generators
→ Stub Renderer
→ File Writer

The package does not simply replace words inside a template.
The real value is the conversion pipeline:

columns / types / nullable / defaults
→ fillable / casts / validation rules
→ generated Laravel files

Features

  • Generate Eloquent models from existing database tables
  • Generate $fillable from table columns
  • Generate model casts from database column types
  • Generate Store and Update FormRequest rules
  • Generate Store and Update DTO classes from validated data
  • Generate Store and Update Action classes
  • Generate API resources
  • Generate API resource controllers with index, store, show, update, and destroy
  • Support --dry-run preview
  • Support JSON dry-run plans for tooling
  • Support --force overwrite
  • Optional API route, smoke test, and relationship generation
  • Publishable config
  • Publishable stubs

Requirements

  • PHP 8.2+
  • Laravel 12 or 13
  • Composer

Installation

Install the package via Composer:

composer require cxuan1225/laravel-api-from-table --dev

This package is intended to be used during development, so installing it with --dev is recommended.

Usage

Generate model, requests, DTOs, actions, resource, and API controller from a database table:

php artisan api:from-table customers

The generated controller is API-oriented and wires requests, DTOs, actions, and resources together:

public function store(StoreCustomerRequest $request): CustomerResource
{
    $customer = $this->storeCustomerAction->handle(
        StoreCustomerData::fromRequest($request),
    );

    return new CustomerResource($customer);
}

Dry Run

Preview the generated files without writing them:

php artisan api:from-table customers --dry-run

For machine-readable previews:

php artisan api:from-table customers --dry-run --json

The JSON output lists planned files, warnings, skipped files, route targets, and test targets.

Force Overwrite

Overwrite existing generated files:

php artisan api:from-table customers --force

Generate Only Model

php artisan api:from-table customers --model

Generate Only Requests

php artisan api:from-table customers --requests

Generate Only DTOs

php artisan api:from-table customers --dto

Generate Only Actions

php artisan api:from-table customers --actions

Generate Only API Resource

php artisan api:from-table customers --resource

Generate Only API Controller

php artisan api:from-table customers --controller

Generate Routes

Append an API resource route to the configurable routes path, which defaults to routes/web.php:

php artisan api:from-table customers --routes

Append the route to routes/api.php:

php artisan api:from-table customers --api-routes

Generated resource URIs use kebab case by default, so a legacy_users table generates:

Route::apiResource('legacy-users', \App\Http\Controllers\LegacyUserController::class);

Generate Smoke Tests

Generate Pest endpoint smoke tests:

php artisan api:from-table customers --tests

When --tests is combined with --api-routes, generated test requests use the configurable API prefix, which defaults to /api:

php artisan api:from-table legacy_users --api-routes --tests
$this->getJson('/api/legacy-users')->assertOk();

Generate Relationships

Relationship generation is opt-in. When enabled, single-column foreign keys can generate belongsTo, inverse hasMany, and whenLoaded resource fields:

php artisan api:from-table orders --relationships

Example

Given this database table:

CREATE TABLE customers (
    id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    company_id BIGINT UNSIGNED NOT NULL,
    name VARCHAR(255) NOT NULL,
    email VARCHAR(255) NULL,
    credit_limit DECIMAL(12,2) DEFAULT 0,
    is_active TINYINT(1) DEFAULT 1,
    created_at TIMESTAMP NULL,
    updated_at TIMESTAMP NULL
);

The package can generate a model like this:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

final class Customer extends Model
{
    protected $fillable = [
        'company_id',
        'name',
        'email',
        'credit_limit',
        'is_active',
    ];

    protected function casts(): array
    {
        return [
            'company_id' => 'integer',
            'credit_limit' => 'decimal:2',
            'is_active' => 'boolean',
        ];
    }
}

It can also generate FormRequest rules:

public function rules(): array
{
    return [
        'company_id' => ['required', 'integer'],
        'name' => ['required', 'string', 'max:255'],
        'email' => ['nullable', 'string', 'email', 'max:255'],
        'credit_limit' => ['nullable', 'numeric'],
        'is_active' => ['nullable', 'boolean'],
    ];
}

Command Options

Option Description
--dry-run Preview generated code without writing files
--force Overwrite existing files
--model Generate only the model
--requests Generate only FormRequest files
--dto Generate only DTO files
--actions Generate only Action files
--resource Generate only the API Resource file
--controller Generate only the API Controller file
--routes Append a Route::apiResource(...) entry to the configured routes path
--api-routes Append a Route::apiResource(...) entry to routes/api.php
--relationships Generate opt-in Eloquent relationships and whenLoaded resource fields from foreign keys
--tests Generate a Pest endpoint smoke test
--json Emit JSON output when combined with --dry-run
--all Generate all supported files, ignoring disabled defaults

Configuration

Publish the config file:

php artisan vendor:publish --tag=api-from-table-config

The config file will be published to:

config/api-from-table.php

Route naming and smoke-test prefixes can be customized:

'routes' => [
    'resource_name_style' => 'kebab',
    'prefixes' => [
        'routes' => '',
        'api_routes' => 'api',
    ],
],

Custom Stubs

Publish the stubs:

php artisan vendor:publish --tag=api-from-table-stubs

The stubs will be published to:

stubs/vendor/api-from-table

You may customize these stub files to match your own Laravel project style.

Available stubs:

model.stub
request.store.stub
request.update.stub
data.store.stub
data.update.stub
action.store.stub
action.update.stub
resource.stub
controller.api.stub

Current Status

The first release focuses on generating a practical Laravel API layer from one existing database table:

  • Model with $fillable, casts, and non-conventional table name support
  • Automatic $hidden output for sensitive fields and hashed password casts
  • Store and Update FormRequest classes
  • Store and Update DTO classes
  • Store and Update Action classes
  • API Resource class
  • API Controller with index, store, show, update, and destroy
  • Optional routes/web.php or routes/api.php route snippet and stronger Pest smoke test generation
  • Optional foreign-key relationship generation with resource whenLoaded output
  • Shared route resolver for generated route names and test URIs
  • --dry-run, --force, and file-type-only generation options
  • --dry-run --json plans for tooling
  • Publishable config and stubs

Roadmap

Schema Intelligence

  • Foreign key detection
  • Check constraint parsing across more database drivers

API Ergonomics

  • Optional policy generation
  • Smarter validation rules from column names such as url

Customization

  • More granular config for generated namespaces and class names
  • Optional timestamps in resources
  • Optional typed DTO properties

Testing

Run the test suite:

composer test

Or run Pest directly:

vendor/bin/pest

License

Laravel API From Table is open-sourced software licensed under the MIT license.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-05-09

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固