applejack21 / laravel-resource-builder
A Laravel package that generates action, controller, policy, and request files for the specified model.
Package info
github.com/Applejack21/laravel-resource-builder
pkg:composer/applejack21/laravel-resource-builder
Requires
- php: >=8.2
- illuminate/console: >=11.0
- illuminate/contracts: >=11.0
- illuminate/database: >=11.0
- illuminate/support: >=11.0
- illuminate/validation: >=11.0
Requires (Dev)
None
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Laravel Resource Builder is a package that scaffolds a full set of resource files for an existing Eloquent model with a single Artisan command. Rather than hand-writing or copy and pasting an action, controller, policy and form requests every time you add a new model, point the command at a model and table, and it will generate the files for you, complete with validation rules inferred from your database schema.
What it generates
Running the build command for a model creates the following files:
| File | Purpose | Location |
|---|---|---|
Create{Model} |
Action to create a new model. | app/Actions/{Model}/Create{Model}.php |
Update{Model} |
Action to update an existing model. | app/Actions/{Model}/Update{Model}.php |
Delete{Model} |
Action to delete (or force delete) a model. | app/Actions/{Model}/Delete{Model}.php |
Get{Models} |
Action to fetch models, with optional pagination and filtering. | app/Actions/{Model}/Get{Models}.php |
{Model}Controller |
A resourceful controller wired up to the actions and form requests above. | app/Http/Controllers/{Model}/{Model}Controller.php |
{Model}Policy |
An authorisation policy with all standard abilities stubbed out. | app/Policies/{Model}Policy.php |
Store{Model}Request |
Form request for storing a new model, with generated validation rules. | app/Http/Requests/{Model}/Store{Model}Request.php |
Update{Model}Request |
Form request for updating a model, with generated validation rules. | app/Http/Requests/{Model}/Update{Model}Request.php |
If any of these files already exist, you will be asked whether you want to overwrite them before anything is touched.
Requirements
- PHP 8.2+
- Laravel 11.x+
Installation
Install the package via Composer:
composer require applejack21/laravel-resource-builder
The package's service provider is auto-discovered, so no further registration is required.
Usage
Generating resource files
To generate the resource files for a model, run:
php artisan laravel-resource-builder:create {model}
For example:
php artisan laravel-resource-builder:create Post
If you don't supply the model argument, you will be prompted for it. Before anything is generated, the command displays the resolved model class and table name and asks you to confirm they're correct.
The command accepts the following options:
| Option | Description | Default |
|---|---|---|
--model-dir= |
The namespace the model lives in. Use double backslashes (\\) to separate the namespace segments. |
App\Models |
--table-name= |
The database table to inspect for the model. | The model's getTable() result |
Example using both options:
php artisan laravel-resource-builder:create Post --model-dir="App\\Models\\Blog" --table-name=blog_posts
How validation rules are generated
When building the store and update form requests, the package inspects the model's table schema and infers sensible validation rules automatically, including:
requiredornullable, based on whether the column allowsNULLRule::unique(...)for columns with a unique index (ignoring the current model's row on the update request)Rule::exists(...)for columns ending in_idthat have a foreign key constraintmax:{length}for columns with a defined character length- Type-based rules such as
string,boolean,dateandinteger - Name-based rules for common column names, including
email,url,password(addsconfirmed),terms(addsaccepted),uuid,ulid, and paired date/datetime columns such asstart_date/end_dateorstarts_at/ends_at(addsbefore:/after:constraints on each other)
The primary key, timestamp columns, deleted_at, remember_token and any two_factor_* columns are skipped automatically, as these aren't expected to be supplied by the request.
MySQL/MariaDB, PostgreSQL and SQLite connections are all supported for schema inspection.
Customising the generated files
The files above are generated from stub templates bundled with the package. If you'd like to tweak the structure, imports or boilerplate of the generated files, you can publish the stubs into your own application and edit them freely; once published, your copies take precedence over the package's own stubs.
Publish the stubs with:
php artisan laravel-resource-builder:copy-stubs
This copies the stub files into a stubs/ directory at the root of your application, mirroring the package's own structure:
stubs/
├── actions/
│ ├── create_action.stub
│ ├── delete_action.stub
│ ├── get_action.stub
│ └── update_action.stub
├── controller/
│ └── create_controller.stub
├── policy/
│ └── create_policy.stub
└── requests/
├── store_request.stub
└── update_request.stub
If a stub already exists at the destination, you'll be asked whether you want to overwrite it.
Available placeholders
Each stub is a plain PHP file containing placeholders that are replaced when a resource is generated. These are the placeholders available to use in your own copies of the stubs:
| Placeholder | Replaced with |
|---|---|
{{ namespace }} |
The namespace for the file being generated, e.g. App\Actions\Post. |
{{ model }} |
The studly-case model name, e.g. Post. |
{{ lowerModel }} |
The camel-case model name, e.g. post. Used for variable names. |
{{ pluralModel }} |
The plural, studly-case model name, e.g. Posts. Used for the get action. |
{{ pluralLowerModel }} |
The plural, camel-case model name, e.g. posts. Used for variable names. |
{{ policyModel }} |
The variable name used for the model in policy methods. This is model when the model is User, to avoid a naming clash with the injected User $user parameter, and {{ lowerModel }} otherwise. |
{{ createValidationRules }} |
The generated validation rules array for the store request. |
{{ updateValidationRules }} |
The generated validation rules array for the update request. |
Examples
The examples folder contains a full set of output generated by running laravel-resource-builder:create against the User model that ships by default with a new Laravel 13.x application (the name, email, email_verified_at and password columns from the default users migration). It's a good reference for what to expect before you run the command against your own models:
CreateUser.php,UpdateUser.php,DeleteUser.phpandGetUsers.php— the generated actionsUserController.php— the generated controllerUserPolicy.php— the generated policyStoreUserRequest.phpandUpdateUserRequest.php— the generated form requests, with validation rules inferred from theuserstable
Contributing
This package is free and open-source software, and contributions are very welcome. If you'd like to add a feature, fix a bug, or improve the documentation, feel free to fork the repository and open a pull request. If you spot an issue, please open one on the repository so it can be discussed.
Licence
This package is open-source software licensed under the MIT licence.