PHPackages                             cxuan1225/laravel-api-from-table - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. cxuan1225/laravel-api-from-table

ActiveLibrary[Database &amp; ORM](/categories/database)

cxuan1225/laravel-api-from-table
================================

Generate Laravel API classes from existing database tables.

v0.3.0(2mo ago)02MITPHPPHP ^8.2

Since May 9Pushed 2mo agoCompare

[ Source](https://github.com/Cxuan1225/Laravel-API-From-Table)[ Packagist](https://packagist.org/packages/cxuan1225/laravel-api-from-table)[ RSS](/packages/cxuan1225-laravel-api-from-table/feed)WikiDiscussions master Synced 1w ago

READMEChangelogDependencies (7)Versions (4)Used By (0)

Laravel API From Table
======================

[](#laravel-api-from-table)

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
-----------------------

[](#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
---------------

[](#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
---------

[](#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
--------

[](#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
------------

[](#requirements)

- PHP 8.2+
- Laravel 12 or 13
- Composer

---

Installation
------------

[](#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
-----

[](#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
-------

[](#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
---------------

[](#force-overwrite)

Overwrite existing generated files:

```
php artisan api:from-table customers --force
```

---

Generate Only Model
-------------------

[](#generate-only-model)

```
php artisan api:from-table customers --model
```

---

Generate Only Requests
----------------------

[](#generate-only-requests)

```
php artisan api:from-table customers --requests
```

---

Generate Only DTOs
------------------

[](#generate-only-dtos)

```
php artisan api:from-table customers --dto
```

---

Generate Only Actions
---------------------

[](#generate-only-actions)

```
php artisan api:from-table customers --actions
```

---

Generate Only API Resource
--------------------------

[](#generate-only-api-resource)

```
php artisan api:from-table customers --resource
```

---

Generate Only API Controller
----------------------------

[](#generate-only-api-controller)

```
php artisan api:from-table customers --controller
```

---

Generate Routes
---------------

[](#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-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
----------------------

[](#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
-------

[](#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:

```
