PHPackages                             kroderdev/laravel-schema-helper - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. kroderdev/laravel-schema-helper

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

kroderdev/laravel-schema-helper
===============================

Generate portable form schemas from Laravel FormRequests

v1.1.0(10mo ago)069MITPHPPHP &gt;=8.1

Since Jun 17Pushed 10mo agoCompare

[ Source](https://github.com/KroderDev/laravel-schema-helper)[ Packagist](https://packagist.org/packages/kroderdev/laravel-schema-helper)[ RSS](/packages/kroderdev-laravel-schema-helper/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (2)Dependencies (3)Versions (3)Used By (0)

Laravel-Schema-Helper
=====================

[](#laravel-schema-helper)

[![Packagist Version](https://camo.githubusercontent.com/43a153dd4b18a4143a314a8c97da82075b67b0e07f5cf187d5db5184bbaaee55/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6b726f6465726465762f6c61726176656c2d736368656d612d68656c7065722e737667)](https://packagist.org/packages/kroderdev/laravel-schema-helper) [![Downloads](https://camo.githubusercontent.com/c27473212fb80410c9c122fd6039834bf02bf2996c298e1bcd240d84b827d7a8/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6b726f6465726465762f6c61726176656c2d736368656d612d68656c7065722e737667)](https://packagist.org/packages/kroderdev/laravel-schema-helper) [![License](https://camo.githubusercontent.com/64f9062ab01334b4b1cba375f25a9c0434446cb8d55cd6c429cf8cda86f212fe/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f6b726f6465726465762f6c61726176656c2d736368656d612d68656c7065722e737667)](LICENSE)

Generate portable form schemas from Laravel FormRequests

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

[](#installation)

```
composer require kroderdev/laravel-schema-helper
```

### Optional: Publish the Configuration File

[](#optional-publish-the-configuration-file)

To customize the default behavior of the Schema Helper package, you may publish the configuration file:

```
php artisan vendor:publish --tag=config --provider="Kroderdev\SchemaHelper\SchemaServiceProvider"
```

Basic Usage
-----------

[](#basic-usage)

```
use Kroderdev\SchemaHelper\SchemaHelper;

$schema = SchemaHelper::generateFromRequest(new ModelStoreRequest())
    ->withOptions([
        'my-select' => ['1' => 'Option 1', '2' => 'Option 2' ...],
    ])
    ->toArray();
```

Available Exports
-----------------

[](#available-exports)

- `toArray()`: Returns the schema as a PHP array.
- `toJson()`: Returns the schema as a JSON string.
- `toJsonResponse()`: Returns the schema as a Laravel JSON response.
- `toVueSchema()`: Returns the schema formatted for Vue components. (Alpha, test required)
- `toReactSchema()`: Returns the schema formatted for React components. (Alpha, test required)

Output Example
--------------

[](#output-example)

```
[
    {
        "name": "item_name",
        "type": "string",
        "required": true,
        "label": "Item Name",
    },
    {
        "name": "item_code",
        "type": "string",
        "required": true,
        "label": "Item Code",
    },
    {
        "name": "category",
        "type": "select",
        "required": true,
        "label": "Category",
        "options": {
        "1": "Option A",
        "2": "Option B",
        "3": "Option C",
        "4": "Option D",
        "5": "Option E",
        "6": "Option F"
        }
    },
    {
        "name": "is_component",
        "type": "checkbox",
        "required": false,
        "label": "Is Component",
    },
]
```

Route Registration Modes
------------------------

[](#route-registration-modes)

By default, all schema endpoints are registered **inline** under the `api` middleware, using the prefix defined in `config/schema-helper.php`.

### Inline Mode (default)

[](#inline-mode-default)

```
Route::middleware('api')->prefix('api/schemas')->group(function() {
    Route::get('/', …)->name('schemas.index');
    Route::get('user', …)->name('schemas.user');
    // etc.
});
```

- **Toggle it off** by setting in your `.env`:

```
SCHEMA_HELPER_ROUTE_MODE=file
```

### File Mode

[](#file-mode)

If you prefer to keep your routes in a separate file, switch to **file** mode:

1. Publish the config and set the mode:

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

    ```
    SCHEMA_HELPER_ROUTE_MODE=file
    SCHEMA_HELPER_ROUTES_FILE=routes/schema-helper.php
    ```
2. Create `routes/schema-helper.php` in your app:

    ```
