Laravel Code Generator

Introduction

A clean code generator for Laravel framework that will save you time! This awesome tool will help you generate resources like views, controllers, routes, migrations, languages or request-forms is seconds! It is extremely flexible and customizable to cover many use cases. It is shipped with cross-browsers compatible template, along with a client-side validation to modernize your application. The source code of this project can be found at GitHub and available under MIT license

Features

  • Create very clean, reusable and highly readable code to build on.
  • Create full resources using a single command with migration or from existing database.
  • Creates full resources for all of the existing tables in the database using one command.
  • Allows you to save the fields in a JSON file and recreate resources when the business needs changes.
  • Utilizes JSON based resource-file to allow you to define your resources. Resource-file allows you to easily regenerate the resource at any time even when the business rules change.
  • Create standard CRUD controllers with simple or form-request validation.
  • Customizable view’s templates to enable you to change the standard look and feel of your application.
  • Create model with relations.
  • Create named routes with and without group.
  • Create standard CRUD views.
  • Smart migration engine! Keeps track of all generated migrations to only create the needed migration.
  • Intelligent enough to automatically handles the relations between the models.
  • Very flexible and rich with configurable options.
  • Easy commands to create resource-file; additionally, add or reduce existing resource-file.
  • Full capability to generate multi-languages applications.
  • Client-side validation.
  • File uploading handling.
  • Auto store multiple-response in the database.
  • Create form-request to clean up your controller and increase your code reusability.
  • Create view's layouts with and without client-side validation.
  • Change the template at run time to generate different views.
  • Ability to generate views with and without Laravel-Collective.
  • Nicely handles any date, time or datetime field.
  • Auto handles any boolean field.

Live Demo

A live demo of a site which was generated using Laravel code generator v2.2 can be found here.

This demo was generated using the following two commands

php artisan resource-file:create Biography --fields=name,age,biography,sport,gender,colors,is_retired,photo,range,month
php artisan create:resources Biography --with-form-request --models-per-page=15 --with-migration

This demonstration allows you to list, add, update or delete records. However, it will not commit anything to the database.

Dependencies

Prerequisite

Default template dependencies


(Optional) Client-side validation dependencies


(Optional) Using Laravel-Collective

Laravel-Code-Generator is fully capable of generating views using Laravel-Collective. To Generate views using it, you must first install Laravel-Collective into your project. Instructions on how to use it can be found below


Getting Started

Installation

1) To download this package into your Laravel project, use the command-line to execute the following command

composer require crestapps/laravel-code-generator --dev

2) (You may skip this step when using Laravel >= 5.5) To bootstrap the packages into your project while using command-line only, open the app/Providers/AppServiceProvider.php file in your project. Then, add the following code to the register() method.

if ($this->app->runningInConsole()) {
    $this->app->register('CrestApps\CodeGenerator\CodeGeneratorServiceProvider');
}

3) Execute the following command from the command-line to publish the package's config and the default template to start generating awesome code.

php artisan vendor:publish --provider="CrestApps\CodeGenerator\CodeGeneratorServiceProvider" --tag=default

A layout is required for the default views! The code generator allows you to create a layout using the command-line. Of course you can use your own layout. You'll only need to include CSS bootstrap framework in your layout for the default templates to work properly. Additionally, you can chose to design your own templates using a different or no css framework. For more info on how to create a custom template click here!

Real life example

Below a video to demonstrate how to use most of the package commands.

Available Commands

The option in between the square brackets [] must be replaced with a variable of your choice.

  • Main commands
    • php artisan create:layout [application-name]
    • php artisan create:resources [model-name]
    • php artisan create:controller [model-name]
    • php artisan create:model [model-name]
    • php artisan create:form-request [model-name]
    • php artisan create:routes [model-name]
    • php artisan create:migration [model-name]
    • php artisan create:language [model-name]
    • php artisan create:mapped-resources
  • Views commands
    • php artisan create:views [model-name]
    • php artisan create:index-view [model-name]
    • php artisan create:create-view [model-name]
    • php artisan create:edit-view [model-name]
    • php artisan create:show-view [model-name]
    • php artisan create:form-view [model-name]
  • Resource's files commands
    • php artisan resource-file:from-database [model-name]
    • php artisan resource-file:create [model-name]
    • php artisan resource-file:append [model-name]
    • php artisan resource-file:reduce [model-name]
    • php artisan resource-file:delete [model-name]
  • Migration commands
    • php artisan migrate-all
    • php artisan migrate:rollback-all
    • php artisan migrate:reset-all
    • php artisan migrate:refresh-all
    • php artisan migrate:status-all

Examples

The following example assumes that we are trying to create a CRUD called AssetCategory with the fields listed below.

  • id
  • name
  • description
  • is_active

#1 Basic example

php artisan resource-file:create AssetCategory --fields=id,name,description,is_active

The above command will create resource-file names /resources/codegenerator-files/asset_categories.json

php artisan create:resources AssetCategory

The above command will create a model app/Models/AssetCategory, a controller app/Http/Controllers/AsseyCategoriesController, all views, the routes, and migration file!

#2 Basic example using translations for English and Arabic

php artisan resource-file:create AssetCategory --fields=id,name,description,is_active --translation-for=en,ar

The above command will create resource-file names /resources/codegenerator-files/asset_categories.json

php artisan create:resources AssetCategory

The above command will create a model app/Models/AssetCategory, a controller app/Http/Controllers/AsseyCategoriesController, all views, the routes, and migration file!

#3 Basic example with form-request

php artisan resource-file:create AssetCategory --fields=id,name,description,is_active

The above command will create resource-file names /resources/codegenerator-files/asset_categories.json

php artisan create:resources AssetCategory --with-form-request

The above command will create a model app/Models/AssetCategory, a controller app/Http/Controllers/AsseyCategoriesController, all views, the routes, and migration file!

#4 Basic example with soft-delete and migration

php artisan resource-file:create AssetCategory --fields=id,name,description,is_active

The above command will create resource-file names /resources/codegenerator-files/asset_categories.json

php artisan create:resources AssetCategory --with-soft-delete --with-migration

The above command will create a model app/Models/AssetCategory, a controller app/Http/Controllers/AsseyCategoriesController, all views, the routes, and migration file!

#5 Creating resources from existing database.

php artisan create:resources AssetCategory --table-exists

The above command will create resource-file names /resources/codegenerator-files/asset_categories.json

Then it will create a model app/Models/AssetCategory, a controller app/Http/Controllers/AsseyCategoriesController, all views and the routes!

You may also create a resource-file from existing database separately using php artisan resource-file:form-database AssetCategory

#6 Creating resources from existing database with translation for English and Arabic

php artisan create:resources AssetCategory --table-exists --translation-for=en,ar

The above command will create resource-file names /resources/codegenerator-files/asset_categories.json

Then it will create a model app/Models/AssetCategory, a controller app/Http/Controllers/AsseyCategoriesController, all views and the routes!

You may also create a resource-file from existing database separately using php artisan resource-file:form-database AssetCategory --translation-for=en,ar

#7 Creating resources from existing database with translation for English and Arabic in two step for better control over the fields!

php artisan resource-file:form-database AssetCategory --translation-for=en,ar

php artisan create:resources AssetCategory

The above command will create resource-file names /resources/codegenerator-files/asset_categories.json

Then it will create a model app/Models/AssetCategory, a controller app/Http/Controllers/AsseyCategoriesController, all views and the routes!

Important Naming Convention

Laravel-Code-Generator strive to generate highly readable, and error free code. In order to keep your code readable, it is important to follow a good naming convention when chosing names for your models, fields, tables, relations and so on. Here is a list of recommendation that we belive is important to keep your code clean and highly readable.

  1. Since each model represents a single object/row in a list/database, naming the model should be written in singular-form while using Studly Case. For example, Post and PostCategory...
  2. Since a database is a collection of model's object, table naming should always be plural and written in lowercase while using Snake Case. For example, users, post_categories...
  3. Primary keys should be named id in the table.
  4. Since the foreign key represents a foreign/other table, the name should always end with _id. For example, post_id, user_id, post_category_id...
  5. Field naming should always be in a singular-form and written in lowercase while using Snake Case. For example, title, first_name, description...

What's new?

Release Notes

Version 2.2 introduces very exciting features, more flexibility and less work for you out of the box! It also, adds support for the new features that were introduced in Laravel 5.5. Follow is a list of all new features and changes that were introduced.

 

New Futures

Smart Migrations Engine

Whaaaat?!! Yup that's right, version 2.2 introduce a very powerful feature which keeps track of all your migrations. After migrating, each time, you add/delete a field/index from your resource file, the code-generator will only generate a migration to add/drop and drop/add columns as needed! Keep in mind that you still have to tell the generator that you need to create a new migration using create:migration command or the --with-migration option for the create:resources command.

Another migration related feature was to organizing your migration files! When uses migrations heavily, finding a specific migration may be overwhelming due to the number of file. This feature, allow you to group all your migrations into sub-folders. Please note that this feature is off by default, to turn it on, set organize_migrations to true.

You're probably thinking "Laravel only detects migrations in the main folder... boooo!" That is correct! However, if you are using Laravel 5.3+, version 2.2 of Laravel-code-generator include five new commands to help you interact with migration from all folders. Check out the "Command Changes" below for more info about the new commands.

Previously Laravel-Code-Generator was limited to belongsTo() type relation. Now, when creating resources from existing database's table, the code-generator is able to create hasOne() and hasMany() relations by scanning the database's constrains and analyzing its existing data.

In the resource-file you can now define any Eloquent relations. Each relation should follow the foreign-relation schema below. Additionally, you can define composite/multi-columns indexes! Each index should follow the index schema listed below.

When using Laravel 5.5, you can pass custom Validation Rule object directly in you resource file and the generator will add it to the validation rules! For more info check out the validation option below

Improved the file uploading process to allow you to delete uploaded file

--indexes and --relations have been added to the following commands resource-file:create, resource-file:append, or resource-file:reduce to allow you to interact with the resource-file freely.

The options --fields, --indexes and --relations for the resource-file:create, resource-file:append, or resource-file:reduce commands accept complex string to allow you to pass more values to add to the resource-file. For example, --fields="name:colors;html-type:select;options:blue|yellow|green|red|white,name:second_field_name"

More configurations so you can type less and do more!

plural_names_for was added to the configuration file to allow you to set your own plural-form vs singular-form preference when naming controller, form-request, resource-file, language file, table-name and route group. If you like your controllers to be in a plural-form, you can simply change the default behavior from the configuration file!

controller_name_postfix was added to the configuration file to allow you to change the controller's postfix. If you don't like to post fix your controllers with the word Controller, you can set this to an empty string or any other value.

form_request_name_postfix was added to the configuration file to allow you to change the form-request's postfix. If you don't like to post fix your form-request with the word FormRequest, you can set this to an empty string or any other value.

irregular_plurals was added to the configuration file. The code-generator heavily uses Laravel helpers str_plural() and str_singular() to generate readable code to make your code spectacular. The problem is the both generate incorrect words for irregular plurals. If you are using a language other than English, you can define a word with each with its plural-form to help the generator keep your code readable.

create_move_file_method was added to the configuration file. This option will allow the user to chose not to create moveFile method on every CRUD when file-upload is required. If you set this to false, it is your responsibility make sure that the moveFile method exists in a higher level of your code like App\Http\Controllers\Controller.

New configuration file (i.e config/code_generator_custom.php) was added to allow you to override the default configuration. This way, you won't lose any of your custom configuration when upgrating which is important! For more info, read the config file.

Cleaner!

In addition to storing fields in the JSON file, indexes and relations can be stored in the same file too! For that reason, the option --fields-file have been renamed to --resource-file in all the commands.

Version 2.2 completly dropped support for raw fields, indexes, and relations as announced in previous documents. Storing resources in JSON file is much better, easier to manage, easier to regenerate resources in the future, shorter/cleaner commands, and much more flexible!

Thanks to the request validation improvment in Laravel 5.5, the controller code is much cleaner.

When the ConvertEmptyStringsToNull middleware is registered, we no longer convert empty string to null manually since the middleware will do just that.

The --without-migration option with php artisan create:resources command has been reversed. It is now --with-migration and should only be passed when you need a new migration created.

For consistency, the --lang-file-name option have been renamed to --language-filename.

The options --names in the resource-file:create, resource-file:append, and resource-file:reduce has been renamed to --fields.

Command Changes

The following commands were renamed

The command create:fields-file has been renamed to resource-file:from-database

The command fields-file:create has been renamed to resource-file:create

The command fields-file:delete has been renamed to resource-file:delete

The command fields-file:append has been renamed to resource-file:append

The command fields-file:reduce has been renamed to resource-file:reduce

The following commands were added

php artisan migrate-all command was adde. It allow you to run all of your outstanding migrations from all folders

php artisan migrate:rollback-all command was added and it allows you to rolls back the last "batch" of migrations, which may include multiple migration from all folders.

php artisan migrate:reset-all command was added to allow you to roll back all of your application's migrations from all folder.

php artisan migrate:refresh-all command was added to allow you to invoke the migrate:rollback-all command then immediately invokes the migrate:migrate-all command.

php artisan migrate:status-all command was added to allow you to checks the status of all your migration from all folders.

Bug Free!

All known bugs have been addressed!

Upgrade Guide

  • In your composer.json file, update the crestapps/laravel-code-generator dependency to 2.2.*.
  • Using the command-line, execute the following two commands to upgrade to the lastest version of v2.2
    • composer update
    • php artisan vendor:publish --provider="CrestApps\CodeGenerator\CodeGeneratorServiceProvider" --tag=default --force
  • If you will be using Laravel-Collective, execute the following commands update the default-collective template.
    • php artisan vendor:publish --provider="CrestApps\CodeGenerator\CodeGeneratorServiceProvider" --tag=default-collective --force
  • Move any custom template "if any" from resouces/codegenerator-templates to resources/laravel-code-generator/templates. IMPORTANT do not copy the default and default-collective folders.
  • Move all the file that are located in resouces/codegenerator-files to resources/laravel-code-generator/sources. Now you should be able to delete the following two folders since they have been relocated resouces/codegenerator-templates and resouces/codegenerator-files.
  • Finally, there are some changes to the layout stub which are required. To override your existing layout call the following codephp artisan create:layout "My New App". If you are using your own layout, you may want to create a temporary layout and extract the updated css/js code into your own layout/assets. The following command will create a new file called "app_temp.blade.php" php artisan create:layout "My New App" --layout-filename=app_temp

How To...


All examples below assumes that you already created a resource-file (i.e resources/codegenerator-fields/posts.json. This file can be created using the following command php artisan resource-file:create Post --fields=id,title,details,is_active)

How to create "views-layout"?

Command
php artisan create:layout [application-name]
Description
Create a new layout for your application.
Example
php artisan create:layout "My New Laravel App"
application-name

The name of your application.

--layout-filename

Default: app

The name of the layout file to be used.

In this example the layout file will be "app.blade.php"

--layout-directory

Default: layouts

The directory to create the layout under.

--template-name

This option allows you to use a different template at run time.

If this option is left out, the default template is used.

Note: the default template can be set from the config file (i.e config/codegenerator.php) by setting the template key to a different value.

--without-validation

This option allows you to create a layout without any client-side validation

Note: client-side validation will boost your app performance.

--force

This option will override the layout if one already exists.

How to create resources?

Command
php artisan create:resources [model-name]
Description
Create multiple resources at the same time. It can be invioked every time the resource-file is modified to recreate the resources all over again.
Example
php artisan create:resources Post
model-name

The model name to create resources for.

--resource-file

Default: the plural-form of the model name. If the model name is AssetCategory, the name will then be asset_categories.json

The name of the file to import resource from. This option allows you to have all resources such as fields, indexes and relations in one JSON file, and then import it from the command line. This is a powerful feature which makes it easy to configure the fields, then reuse the same fields in multiple command now or in the future. More documentation on how to manage resource-file can be found in the "Managing fields using JSON file" section.

--controller-name

The name of the controller to create. If the provided value does not end with the word "Controller" it will be appended.

If this option is left out, the controller's name will be generated using the plural-form of the giving model's name.

In the above example, the controller will be called "PostsController".

--controller-extends

Default: Http\Controllers\Controller

Here you can specify which class should the controller extends. By default, we extend Http\Controllers\Controller.

You may pass the value no_value to not extend any class.

--with-auth

When used, this option will add the auth:api to the controller. It will prevent any un-authenticated users to access the resources.

In order to use this option, you must enable Laravel's built in authentication.

--routes-prefix

Default: default-form

Prefix of the route group.

For example, if the word "frontend" was provided, it assumes that all the generated routes start with /frontend/.

By default, the route-prefix is the plural-form of the model name. However, this is something can be changed from the configuration file plural_names_for key.

--models-per-page

Default = 25

The amount of models to show per page on the index view. By default, the "Index" method will display 25 records per page.

--with-form-request

Instead of placing the field's validation rules directly in the controller class, this option will extract the rules into a separate form-request class. The form-request class allows you to do more complex validation, cleans up your controller, and increases your code reusability.

By default, the method authorize() is set to return false for your application's security. This method must be modified to return a true value for the store and update requests to be allowed. Otherwise, the request will be Forbidden

When using --with-auth option, the authorize() method return Auth::check() which should always return true at this point.

--table-name

The database's table name.

If this option is left out, it is assumed that the table name is the plural-form of the model-name.

In the above example, the table name will be "posts".

If the model name is AssetCategory, the table name will be "asset_categories".

--table-exists

This option allows you to generate resources from existing database table.

When this option is used, the database's name is assumes to be the plural-form of the provided "model-name". Of course, the table name can be set to a different value by passing the --table-name option.

When using this option, the command php artisan resource-file:from-database is called behind the scenes to generate a a resource-file first. The name of the generated resource-file will be named the plural-form of the model, unless an explicit name is provided using the --resource-file option. This file will allow you to change the default behavior and recreate the view to fit your needs.

This option is currently available only for MySql database only. It will not work if used with a different driver.

Note: To create multiple-language translation from existing database, use the --translation-for option.

--translation-for

A comma separated languages.

When creating resources from existing database using the --table-exists options, --transation-for allows you to create multi-language labels. You still have to provide translation for the corresponding language but it will get everything setup for you.

If this option is left out, no translation key's will be generated.

For example, passing --translation-for=en,ar,fr will create label under the following languages en, ar and fr

This option will only work when using --table-exists option otherwise it is ignored.
--language-filename

The languages file name to put the labels "if any" in.

If one isn't provided, the file name will be the plural-form of the provided model name.

Note: if the file already exists, and the same key "field name" exists in the file, no message will be added.

This option will only work when using --table-exists option.
--primary-key

Default = id

The field's name of the primary key. The default value can be overridden by setting the is-auto-increment or the is-primary flag to true in the fields setup.

--with-soft-delete

Enables the soft-delete feature that Eloquent provides.

--without-timestamps

Prevent Eloquent from maintaining both created_at and the updated_at properties.

--with-migration

This option will create a migration for your resource.

Behind the scenes, this option invokes the create:migration command to create the required migration.
--migration-class-name

The name of the migration class.

If this option is not set, a name will be generated based on the model name.

--connection-name

Eloquent uses the configured default database connection. Providing a value here will tell Eloquent to connect using the provided connection.

--engine-name

A specific engine name for the database's table can be provided here.

--controller-directory

The directory where the controller should be created under.

For example, if the word "Frontend" was provided, the controller will be created in App/Http/Controllers/Frontend directory.

The default path where the controller will be created can be set from the config file config/codegenerator.php

--model-directory

A directory where the model will be created under.

The default path where the model will be created can be set from the config file config/codegenerator.php

--views-directory

The name of the directory to create the views under.

If this option is left out, the views will be created in /resources/views

--template-name

This option allows you to use a different template at run time.

If this option is left out, the default template is used.

Note: the default template can be set from the config file (i.e config/codegenerator.php) by setting the template key to a different value.

--force

This option will override any existing files if any exists.

How to create multiple resources at once?

Command
php artisan create:mapped-resources
Description
When using resource-file:create, resource-file:from-database and resource-file:delete the resources_map.json file is updated behind the scenes. This options create multiple resources for all the resources found in the resources/laravel-code-generator/sources/resources_map.json at the same time. It can be invioked every time any of the resource-file is modified to recreate the resources all over again.
Example
php artisan create:mapped-resources
--controller-extends

Default: Http\Controllers\Controller

Here you can specify which class should the controller extends. by default, we extend Http\Controllers\Controller.

You may pass the value no_value to not extend any class.

--with-auth

When used, this option will add the auth:api to the controller. It will prevent any un-authenticated uses can access the resources.

In order to use this option, you must enable Laravel's built in authentication.

--models-per-page

Default = 25

The amount of models to show per page on the index view. By default, the "Index" method will display 25 records per page.

--with-form-request

Instead of placing the field's validation rules directly in the controller class, this option will extract the rules into a separate form-request class. The form-request class allows you to do more complex validation, cleans up your controller, and increases your code reusability.

By default, the method authorize() is set to return false for your application's security. This method must be modified to return a true value for the store and update requests to be allowed. Otherwise, the request will be Forbidden

When using --with-auth option, the authorize() method return Auth::check() which should always return true at this point.

--form-request-directory

The directory where the form-request should be created under.

For example, if the word "Frontend" was provided, the form-request will be created in App/Http/Requests/Frontend directory.

The default path where the form-request will be created can be set from the config file config/codegenerator.php

--table-exists

This option allows you to generate resources from existing database table.

When this option is used, the database's name is assumes to be the plural-form of the provided "model-name". Of course, the table name can be set to a different value by passing the --table-name option.

When using this option, the command php artisan resource-file:from-database is called behind the scenes to generate a a resource-file first. The name of the generated resource-file will be named the plural-form of the model, unless an explicit name is provided using the --resource-file option. This file will allow you to change the default behavior and recreate the view to fit your needs.

This option is currently available only for MySql database only. It will not work if used with a different driver.

Note: To create multiple-language translation from existing database, use the --translation-for option.

--translation-for

A comma separated languages.

When creating resources from existing database using the --table-exists options, --transation-for allows you to create multi-language labels. You still have to provide translation for the corresponding language but it will get everything setup for you.

If this option is left out, no translation key's will be generated.

For example, passing --translation-for=en,ar,fr will create label under the following languages en, ar and fr

This option will only work when using --table-exists option otherwise it is ignored.
--language-filename

The languages file name to put the labels "if any" in.

If one isn't provided, the file name will be the plural-form of the provided model name.

Note: if the file already exists, and the same key "field name" exists in the file, no message will be added.

This option will only work when using --table-exists option.
--primary-key

Default = id

The field's name of the primary key. The default value can be overridden by setting the is-auto-increment or the is-primary flag to true in the fields setup.

--with-soft-delete

Enables the soft-delete feature that Eloquent provides.

--without-timestamps

Prevent Eloquent from maintaining both created_at and the updated_at properties.

--with-migration

This option will create a migration for your resource.

Behind the scenes, this option invokes the create:migration command to create the required migration.
--connection-name

Eloquent uses the configured default database connection. Providing a value here will tell Eloquent to connect using the provided connection.

--engine-name

A specific engine name for the database's table can be provided here.

--controller-directory

The directory where the controller should be created under.

For example, if the word "Frontend" was provided, the controller will be created in App/Http/Controllers/Frontend directory.

The default path where the controller will be created can be set from the config file config/codegenerator.php

--model-directory

A directory where the model will be created under.

The default path where the model will be created can be set from the config file config/codegenerator.php

--views-directory

The name of the directory to create the views under.

If this option is left out, the views will be created in /resources/views

--template-name

This option allows you to use a different template at run time.

If this option is left out, the default template is used.

Note: the default template can be set from the config file (i.e config/codegenerator.php) by setting the template key to a different value.

--force

This option will override existing files if any exists.

--mapping-filename

This option allows you to pass the name of the mapping file. When this option is left out, the default resources_map.json file will be used.

How to create a controller?

Command
php artisan create:controller [model-name]
Description
Create a controller for your resource.
Example
php artisan create:controller Posts
model-name

The model name to create the controller for.

--controller-name

The name of the controller to create. If the provided value does not end with the word "Controller" it will be appended.

If this option is left out, the controller name is generated from the provided model name.

In the example above, the controller name will be "PostsController".

--resource-file

Default: the plural-form of the model name. If the model name is AssetCategory, the name will then be asset_categories.json

The name of the file to import resource from. This option allows you to have all resources such as fields, indexes and relations in one JSON file, and then import it from the command line. This is a powerful feature which makes it easy to configure the fields, then reuse the same fields in multiple command now or in the future. More documentation on how to manage resource-file can be found in the "Managing fields using JSON file" section.

--routes-prefix

Default: default-form

Prefix of the route group.

For example, if the word "frontend" was provided, it assumes that all the generated routes start with /frontend/.

By default, the route-prefix is the plural-form of the model name. However, this is something can be changed from the configuration file plural_names_for key.

--models-per-page

Default = 25

The amount of models to show per page on the index view. By default, the "Index" method will display 25 records per page.

--language-filename

The languages file name to put the labels "if any" in.

If one isn't provided, the file name will be the plural-form of the provided model name.

Note: if the file already exists, and the same key "field name" exists in the file, no message will be added.

--controller-extends

Default: Http\Controllers\Controller

Here you can specify which class should the controller extends. by default, we extend Http\Controllers\Controller.

You may pass the value no_value to not extend any class.

--with-auth

When used, this option will add the auth:api to the controller. It will prevent any un-authenticated uses can access the resources.

In order to use this option, you must enable Laravel's built in authentication.

--with-form-request

Instead of placing the field's validation rules directly in the controller class, this option will extract the rules into a separate form-request class. The form-request class allows you to do more complex validation, cleans up your controller, and increases your code reusability.

By default, the method authorize() is set to return false for your application's security. This method must be modified to return a true value for the store and update requests to be allowed. Otherwise, the request will be Forbidden

When using --with-auth option, the authorize() method return Auth::check() which should always return true at this point.

--form-request-directory

The directory where the form-request should be created under.

For example, if the word "Frontend" was provided, the form-request will be created in App/Http/Requests/Frontend directory.

The default path where the form-request will be created can be set from the config file config/codegenerator.php

--controller-directory

The directory where the controller should be created under.

For example, if the word "Frontend" was provided, the controller will be created in App/Http/Controllers/Frontend directory.

The default path where the controller will be created can be set from the config file config/codegenerator.php

--model-directory

A directory where the model will be created under.

The default path where the model will be created can be set from the config file config/codegenerator.php

--views-directory

The name of the directory to create the views under.

If this option is left out, the views will be created in /resources/views

--without-languages

Allow you to create all the resources excluding the language file if one is needed. Note: the language file will only be created if the resource file contains translations.

--without-model

Allow you to create all the resources excluding the model.

--without-controller

Allow you to create all the resources excluding the controller.

--without-form-request

Allow you to create all the resources excluding the form-request if one is used. Note: when creating a controller with a form-request the form-request is injected into the action methods. Thus, in order to create the form-request based controller, you would have to use --with-form-request --with-form-request so the controller know you are using form-request but avoid overriding existing form-request.

--without-views

Allow you to create all the resources excluding the views.

--template-name

This option allows you to use a different template at run time.

If this option is left out, the default template is used.

Note: the default template can be set from the config file (i.e config/codegenerator.php) by setting the template key to a different value.

--force

This option will override the controller if one already exists.

How to create a model?

Command
php artisan create:model [model-name]
Description
Create a model for your CRUD.
Example
php artisan create:model Post
model-name

The name of the model.

--resource-file

Default: the plural-form of the model name. If the model name is AssetCategory, the name will then be asset_categories.json

The name of the file to import resource from. This option allows you to have all resources such as fields, indexes and relations in one JSON file, and then import it from the command line. This is a powerful feature which makes it easy to configure the fields, then reuse the same fields in multiple command now or in the future. More documentation on how to manage resource-file can be found in the "Managing fields using JSON file" section.

--table-name

The database's table name.

If this option is left out, it is assumed that the table name is the plural-form of the model-name.

In the above example, the table name will be "posts".

If the model name is AssetCategory, the table name will be "asset_categories".

--model-directory

A directory where the model will be created under.

The default path where the model will be created can be set from the config file config/codegenerator.php

--primary-key

Default = id

The field's name of the primary key. The default value can be overridden by setting the is-auto-increment or the is-primary flag to true in the fields setup.

--with-soft-delete

Enables the soft-delete feature that eloquent provides.

--without-timestamps

Prevent Eloquent from maintaining both created_at and the updated_at properties.

--template-name

This option allows you to use a different template at run time.

If this option is left out, the default template is used.

Note: the default template can be set from the config file (i.e config/codegenerator.php) by setting the template key to a different value.

--force

This option will override the model if one already exists.

How to create routes?

Command
php artisan create:routes [model-name]
Description
Create routes for your CRUD.
Example
php artisan create:routes Post
model-name

The model name that the routes will represent.

--controller-name

The name of the controller that these routes point to. If the provided value does not end with the word "Controller" it will be appended.

If this option is left out, the controller's name will be generated using the plural-form of the giving model's name.

In the above example, the controller will be called "PostsController".

--routes-prefix

Default: default-form

Prefix of the route group.

For example, if the word "frontend" was provided, it assumes that all the generated routes start with /frontend/.

By default, the route-prefix is the plural-form of the model name. However, this is something can be changed from the configuration file plural_names_for key.

--template-name

This option allows you to use a different template at run time.

If this option is left out, the default template is used.

Note: the default template can be set from the config file (i.e config/codegenerator.php) by setting the template key to a different value.

How to create all standard CRUD views (i.e. Create, Read, Update and Delete)?

When creating views using the create:views, create:create-view or create:update-view an additionally view called "form-view" is created. The "form-view" contains the form fields to prevent code duplication.

Command
php artisan create:views [model-name]
Description
Create "create-view", "edit-view", "index-view", "show-view" and "form-view" for your CRUD at the same time. Behind the scenes it invokes the following commands create:create-view, create:edit-view, create:index-view, create:show-view, and create:form-view command.
Example
php artisan create:views Post
model-name

The model name that the created views will represent.

--resource-file

Default: the plural-form of the model name. If the model name is AssetCategory, the name will then be asset_categories.json

The name of the file to import resource from. This option allows you to have all resources such as fields, indexes and relations in one JSON file, and then import it from the command line. This is a powerful feature which makes it easy to configure the fields, then reuse the same fields in multiple command now or in the future. More documentation on how to manage resource-file can be found in the "Managing fields using JSON file" section.

--routes-prefix

Default: default-form

Prefix of the route group.

For example, if the word "frontend" was provided, it assumes that all the generated routes start with /frontend/.

By default, the route-prefix is the plural-form of the model name. However, this is something can be changed from the configuration file plural_names_for key.

--only-views

Default = create,edit,index,show,form

The only views to be created. A comma separated string with the name of the views to create.

--layout-name

Default = layouts.app

A different layout could be used to generate the views. This can easily be done by providing a different layout name.

For example, if the physical path to a different layout was /resources/views/layouts/template/newlayout.blade.php then its name would be layouts.template.newlayout

--template-name

This option allows you to use a different template at run time.

If this option is left out, the default template is used.

Note: the default template can be set from the config file (i.e config/codegenerator.php) by setting the template key to a different value.

--views-directory

The name of the directory to create the views under.

If this option is left out, the views will be created in /resources/views

--force

This option will override any views if any already exists.

How to create "create-view"?

Command
php artisan create:create-view [model-name]
Description
Create a "create-view" for your CRUD.
Example
php artisan create:create-view Post
model-name

The model name that this view will represent.

--resource-file

Default: the plural-form of the model name. If the model name is AssetCategory, the name will then be asset_categories.json

The name of the file to import resource from. This option allows you to have all resources such as fields, indexes and relations in one JSON file, and then import it from the command line. This is a powerful feature which makes it easy to configure the fields, then reuse the same fields in multiple command now or in the future. More documentation on how to manage resource-file can be found in the "Managing fields using JSON file" section.

--routes-prefix

Default: default-form

Prefix of the route group.

For example, if the word "frontend" was provided, it assumes that all the generated routes start with /frontend/.

By default, the route-prefix is the plural-form of the model name. However, this is something can be changed from the configuration file plural_names_for key.

--layout-name

Default = layouts.app

A different layout could be used to generate the views. This can easily be done by providing a different layout name.

For example, if the physical path to a different layout was /resources/views/layouts/template/newlayout.blade.php then its name would be layouts.template.newlayout

--template-name

This option allows you to use a different template at run time.

If this option is left out, the default template is used.

Note: the default template can be set from the config file (i.e config/codegenerator.php) by setting the template key to a different value.

--views-directory

The name of the directory to create the views under.

If this option is left out, the views will be created in /resources/views

--force

This option will override the view if one already exists.

How to create "edit-view"?

Command
php artisan create:edit-view [model-name]
Description
Create an "edit-view" for your CRUD.
Example
php artisan create:edit-view Post
model-name

The model name that this view will represent.

--resource-file

Default: the plural-form of the model name. If the model name is AssetCategory, the name will then be asset_categories.json

The name of the file to import resource from. This option allows you to have all resources such as fields, indexes and relations in one JSON file, and then import it from the command line. This is a powerful feature which makes it easy to configure the fields, then reuse the same fields in multiple command now or in the future. More documentation on how to manage resource-file can be found in the "Managing fields using JSON file" section.

--routes-prefix

Default: default-form

Prefix of the route group.

For example, if the word "frontend" was provided, it assumes that all the generated routes start with /frontend/.

By default, the route-prefix is the plural-form of the model name. However, this is something can be changed from the configuration file plural_names_for key.

--layout-name

Default = layouts.app

A different layout could be used to generate the views. This can easily be done by providing a different layout name.

For example, if the physical path to a different layout was /resources/views/layouts/template/newlayout.blade.php then its name would be layouts.template.newlayout

--template-name

This option allows you to use a different template at run time.

If this option is left out, the default template is used.

Note: the default template can be set from the config file (i.e config/codegenerator.php) by setting the template key to a different value.

--views-directory

The name of the directory to create the views under.

If this option is left out, the views will be created in /resources/views

--force

This option will override the view if one already exists.

How to create "index-view"?

Command
php artisan create:index-view [model-name]
Description
Create a "index-view" for your CRUD.
Example
php artisan create:index-view Post
model-name

The model name that this view will represent.

--resource-file

Default: the plural-form of the model name. If the model name is AssetCategory, the name will then be asset_categories.json

The name of the file to import resource from. This option allows you to have all resources such as fields, indexes and relations in one JSON file, and then import it from the command line. This is a powerful feature which makes it easy to configure the fields, then reuse the same fields in multiple command now or in the future. More documentation on how to manage resource-file can be found in the "Managing fields using JSON file" section.

--routes-prefix

Default: default-form

Prefix of the route group.

For example, if the word "frontend" was provided, it assumes that all the generated routes start with /frontend/.

By default, the route-prefix is the plural-form of the model name. However, this is something can be changed from the configuration file plural_names_for key.

--layout-name

Default = layouts.app

A different layout could be used to generate the views. This can easily be done by providing a different layout name.

For example, if the physical path to a different layout was /resources/views/layouts/template/newlayout.blade.php then its name would be layouts.template.newlayout

--template-name

This option allows you to use a different template at run time.

If this option is left out, the default template is used.

Note: the default template can be set from the config file (i.e config/codegenerator.php) by setting the template key to a different value.

--views-directory

The name of the directory to create the views under.

If this option is left out, the views will be created in /resources/views

--force

This option will override the view if one already exists.

How to create "show-view"?

Command
php artisan create:show-view [model-name]
Description
Create a "show-view" for your CRUD.
Example
php artisan create:show-view Post
model-name

The model name that this view will represent.

--resource-file

Default: the plural-form of the model name. If the model name is AssetCategory, the name will then be asset_categories.json

The name of the file to import resource from. This option allows you to have all resources such as fields, indexes and relations in one JSON file, and then import it from the command line. This is a powerful feature which makes it easy to configure the fields, then reuse the same fields in multiple command now or in the future. More documentation on how to manage resource-file can be found in the "Managing fields using JSON file" section.

--routes-prefix

Default: default-form

Prefix of the route group.

For example, if the word "frontend" was provided, it assumes that all the generated routes start with /frontend/.

By default, the route-prefix is the plural-form of the model name. However, this is something can be changed from the configuration file plural_names_for key.

--layout-name

Default = layouts.app

A different layout could be used to generate the views. This can easily be done by providing a different layout name.

For example, if the physical path to a different layout was /resources/views/layouts/template/newlayout.blade.php then its name would be layouts.template.newlayout

--template-name

This option allows you to use a different template at run time.

If this option is left out, the default template is used.

Note: the default template can be set from the config file (i.e config/codegenerator.php) by setting the template key to a different value.

--views-directory

The name of the directory to create the views under.

If this option is left out, the views will be created in /resources/views

--force

This option will override the view if one already exists.

How to create "form-view"?

Command
php artisan create:form-view [model-name]
Description
Create a "form-view" for your CRUD.
Example
php artisan create:form-view Post
model-name

The model name that this view will represent.

--resource-file

Default: the plural-form of the model name. If the model name is AssetCategory, the name will then be asset_categories.json

The name of the file to import resource from. This option allows you to have all resources such as fields, indexes and relations in one JSON file, and then import it from the command line. This is a powerful feature which makes it easy to configure the fields, then reuse the same fields in multiple command now or in the future. More documentation on how to manage resource-file can be found in the "Managing fields using JSON file" section.

--routes-prefix

Default: default-form

Prefix of the route group.

For example, if the word "frontend" was provided, it assumes that all the generated routes start with /frontend/.

By default, the route-prefix is the plural-form of the model name. However, this is something can be changed from the configuration file plural_names_for key.

--layout-name

Default = layouts.app

A different layout could be used to generate the views. This can easily be done by providing a different layout name.

For example, if the physical path to a different layout was /resources/views/layouts/template/newlayout.blade.php then its name would be layouts.template.newlayout

--template-name

This option allows you to use a different template at run time.

If this option is left out, the default template is used.

Note: the default template can be set from the config file (i.e config/codegenerator.php) by setting the template key to a different value.

--views-directory

The name of the directory to create the views under.

If this option is left out, the views will be created in /resources/views

--force

This option will override the view if one already exists.

How to create a database migration?

Command
php artisan create:migration [model-name]
Description
Create a migration for your CRUD.
Example
php artisan create:migration Post
model-name

The name of the model.

--table-name

The database's table name.

If this option is left out, it is assumed that the table name is the plural-form of the model-name.

In the above example, the table name will be "posts".

If the model name is AssetCategory, the table name will be "asset_categories".

--resource-file

Default: the plural-form of the model name. If the model name is AssetCategory, the name will then be asset_categories.json

The name of the file to import resource from. This option allows you to have all resources such as fields, indexes and relations in one JSON file, and then import it from the command line. This is a powerful feature which makes it easy to configure the fields, then reuse the same fields in multiple command now or in the future. More documentation on how to manage resource-file can be found in the "Managing fields using JSON file" section.

--migration-class-name

The name of the migration class.

If this option is not set, a name will be generated based on the table name.

--with-soft-delete

Enables the soft-delete feature that Eloquent provides.

--connection-name

Eloquent uses the configured default database connection. Providing a value here will tell Eloquent to connect using the provided connection.

--engine-name

A specific engine name for the database's table can be provided here.

--without-timestamps

Prevent Eloquent from maintaining both created_at and the updated_at properties.

--template-name

This option allows you to use a different template at run time.

If this option is left out, the default template is used.

Note: the default template can be set from the config file (i.e config/codegenerator.php) by setting the template key to a different value.

--force

This option will override the migration if one already exists.

How to create form-request?

Command
php artisan create:form-request [model-name]
Description
Create a form-request for your CRUD.
Example
php artisan create:form-request Post
model-name

The name of the model.

--class-name

The name of the form-request class.

--resource-file

Default: the plural-form of the model name. If the model name is AssetCategory, the name will then be asset_categories.json

The name of the file to import resource from. This option allows you to have all resources such as fields, indexes and relations in one JSON file, and then import it from the command line. This is a powerful feature which makes it easy to configure the fields, then reuse the same fields in multiple command now or in the future. More documentation on how to manage resource-file can be found in the "Managing fields using JSON file" section.

--with-auth

When used, this option will add the auth:api to the controller. It will prevent any un-authenticated users to access the resources.

In order to use this option, you must enable Laravel's built in authentication.

--routes-prefix

Default: default-form

Prefix of the route group.

For example, if the word "frontend" was provided, it assumes that all the generated routes start with /frontend/.

By default, the route-prefix is the plural-form of the model name. However, this is something can be changed from the configuration file plural_names_for key.

--template-name

This option allows you to use a different template at run time.

If this option is left out, the default template is used.

Note: the default template can be set from the config file (i.e config/codegenerator.php) by setting the template key to a different value.

--form-request-directory

The directory where the form-request should be created under.

For example, if the word "Frontend" was provided, the form-request will be created in App/Http/Requests/Frontend directory.

The default path where the form-request will be created can be set from the config file config/codegenerator.php

--force

This option will override the form-request if one already exists.

How to create a language file?

Command
php artisan create:language [model-name]
Description
Create a new language file for your CRUD.
Example
php artisan create:language Post
model-name

The name of the model that these fields will represent.

--language-filename

The language file name to store the keys under. Typically, this would be the plural-form of the model name.

--resource-file

Default: the plural-form of the model name. If the model name is AssetCategory, the name will then be asset_categories.json

The name of the file to import resource from. This option allows you to have all resources such as fields, indexes and relations in one JSON file, and then import it from the command line. This is a powerful feature which makes it easy to configure the fields, then reuse the same fields in multiple command now or in the future. More documentation on how to manage resource-file can be found in the "Managing fields using JSON file" section.

--template-name

This option allows you to use a different template at run time.

If this option is left out, the default template is used.

Note: the default template can be set from the config file (i.e config/codegenerator.php) by setting the template key to a different value.

--force

This option will override any views if any already exists.

How to create resource-file?

Command
php artisan resource-file:create [model-name]
Description
Create a new resource-file.
Example
php artisan resource-file:create Post --fields=id,title,description
model-name

The name of the model that these fields will represent.

--resource-filename

The name of the file to be created. When this option is left out, the file will be the plural-form of the model name.

If the model name is AssetCategory, the file name will be asset_categories.json.

--fields

A list of the field names to be created. The names should be separated by a comma.

You may also pass a complex string using the following schema

--fields="name:colors;html-type:select;options:blue|yellow|green|red|white,name:second_field_name"

Complex string are allowed and will be handy is some cases. However, in most cases all you need to pass is the field names as the common_definitions key in the configuration file will define most options for you out of the box using the name of the field.

Each field in the complex string must be seperated by a ,. Also each property in the field must be seperated by ; while each option of a property is seperated by |.

--relations

A list of the relations to be created. The string should follow the schema below

--relations="name:comments;type:hasMany;field:title;params:App\Models\Comment|post_id|id,"

Each relation in the string must be seperated by a ,. Also each property in the relation must be seperated by ; while each parameter of the params property seperated by |.

--indexes

A list of the indexes to be created. The string should follow the schema below

--indexes="name:first_last_name_index;columns:first_name|last_name,"

Each index in the string must be seperated by a ,. Also each property in the index must be seperated by ; while each field name in the columns property seperated |.

--translation-for

A comma separated languages.

When creating a multi-language application, this option allows for creating translation key in multiple languages. You still have to provide translation for the corresponding language but it will get everything setup for you.

If this option is left out, no translation key's will be generated.

For example, --translation-for=en,ar,fr this option will create label under the following languages en, ar and fr

--force

This option will override the field's file if one already exists.

How to add resources to existing resource-file?

Command
php artisan resource-file:append [model-name]
Description
Appends a new fiels, indexes, or relations to an existing resource-file. If the resource-file does not exists one will be created.
Example
php artisan resource-file:append Post --fields=notes,created_by
model-name

The name of the model that these fields will represent.

--resource-filename

The name of the file to be created. When this option is left out, the file will be the plural-form of the model name.

--fields

A list of the field names to be added. The names should be separated by a comma.

You may also pass a complex string using the following schema

--fields="name:colors;html-type:select;options:blue|yellow|green|red|white,name:second_field_name"

Complex string are allowed and will be handy is some cases. However, in most cases all you need to pass is the field names as the common_definitions key in the configuration file will define most options for you out of the box using the name of the field.

Each field in the complex string must be seperated by a ,. Also each property in the field must be seperated by ; while each option of a property is seperated by |.

--relations

A list of the relations to be added. The string should follow the schema below

--relations="name:comments;type:hasMany;field:title;params:App\Models\Comment|post_id|id,"

Each relation in the string must be seperated by a ,. Also each property in the relation must be seperated by ; while each parameter of the params property seperated by |.

--indexes

A list of the indexes to be added. The string should follow the schema below

--indexes="name:first_last_name_index;columns:first_name|last_name,"

Each index in the string must be seperated by a ,. Also each property in the index must be seperated by ; while each field name in the columns property seperated |.

--translation-for

A comma separated languages.

When creating a multi-language application, this option allows for creating translation key in multiple languages. You still have to provide translation for the corresponding language but it will get everything setup for you.

If this option is left out, no translation key's will be generated.

For example, --translation-for=en,ar,fr this option will create label under the following languages en, ar and fr

How to remove resources to existing resource-file?

Command
php artisan resource-file:reduce [model-name]
Description
Removes fiels, indexes, or relations to an existing resource-file. If the resource-file becomes empty, it will automaticly get deleted by calling the resource-file:delete command.
Example
php artisan resource-file:reduce Post --fields=notes,created_by
model-name

The name of the model that these fields will represent.

--resource-filename

The name of the file to be created. When this option is left out, the file will be the plural-form of the model name.

--fields

A list of the field names to be created. The names should be separated by a comma.

--relations

A comma seperated relation names to remove from the resource file.

--indexes

A comma seperated index names to remove from the resource file.

How to delete existing resource-file?

Command
php artisan resource-file:delete [model-name]
Description
It deletes existing resource-file. It is recommended to use this command to delete file instead of manualy deleting it. This command will also delete the mapped relation in the resource_map file.
Example
php artisan resource-file:delete Post
model-name

The name of the model that these fields represents.

--resource-filename

The name of the file to be created. When this option is left out, the file will be the plural-form of the model name.

How to create a resource's file from existing database?

Are you looking to convert existing application to Laravel framework? Or, looking to use database-first instead of code-first approach? No problem! This package allows you to create a resource's file from existing database.

You can easily take advantage of this feature by passing --table-exists option to the create:resources command to automatically generate all the resources from existing database's table.

Command
php artisan resource-file:from-database [model-name]
Description
It created a new resource-file from existing database. This coomand allow you to convert your existing database into resource file, then the create:resources command is used to generate the resources.
Example
php artisan resource-file:from-database Post
model-name

The name of the model that represents these fields.

--table-name

The name of your existing database's table name.

When this option is left out, the table name is the plural-form of the model name.

If the model name is AssetCategory, the table name is assumed to be asset_categories

--database-name

The database name to look under.

If this option is left out, the database name in the database connection is used.

--resource-file

Default: the plural-form of the model name. If the model name is AssetCategory, the name will then be asset_categories.json

The name of the file to import resource from. This option allows you to have all resources such as fields, indexes and relations in one JSON file, and then import it from the command line. This is a powerful feature which makes it easy to configure the fields, then reuse the same fields in multiple command now or in the future. More documentation on how to manage resource-file can be found in the "Managing fields using JSON file" section.

--resource-filename

The name of the file to generate the fields in.

If this option is left out, the generate name file will be the same name as the database's table.

--translation-for

A comma separated languages.

When creating a multi-language application, this option allows for creating translation key in multiple languages. You still have to provide translation for the corresponding language but it will get everything setup for you.

If this option is left out, no translation key's will be generated.

For example, --translation-for=en,ar,fr this option will create label under the following languages en, ar and fr

--force

This option will override the field's file if one already exists.

How to create a custom template?

One of the powerful feature of this package is that is allows you to create your own custom templates! To create a custom template, simply duplicate /resources/codegenerator-templates/default folder, then name it anything you like. For example, bluesky.

Now, make all the custom changes in the bluesky folder.

To use the bluesky templete, you can pass it when needed for each command using --template-name=bluesky option. Or, if you like to make it the default template for all of you commands, open the config file config/codegenerator.php file, change the value of the template key to bluesky.

Although you can change just about anything in the template, you may only want to change the views related files that ends with .blade.stub

If you like to create a custom template with Laravel-Collective click here for more info.



Resource Files

A JSON based file that allows you to define how you like your resource generated. You can define your fields, indexed, and model relations.

Fields

The minimum requirement for creating a field is a unique name. However, the code-generator is very flexible and allows you to have full control on the fields. Below all the available properties for defining a field

General Properties
name

Required

A unique name for the field.

label

A user-friendly title to describe the field. If this option is left out, the field's name is used to generate a title.

validation

You can pass any valid Laravel validation rule. The rules should be separated by bar |.

For example: required|string|min:2|max:255

Start with Laravel 5.5, you can define custom validation rules and pass them as well. For example, to use a custom validation rule called Uppercase in addition to the required rule, you can pass this string required|new Uppercase.

To learn more about the valid options please visit Laravel documentation

When the rule required is not used, the field in the migration file will automatically become nullable.

HTML Properties
html-type

Default: text

A valid property will be one of the following options

  • text
  • textarea
  • password
  • email
  • checkbox
  • radio
  • number
  • select
  • hidden
  • file
  • selectRange
  • selectMonth
  • multipleSelect

Note: when using file type, after the file is uploaded to the designated path, the filename is stored in the database by default. For everything to work properly, the data-type must be of some sort of a string type. Or modify the behavior of moveFile method to handle the new file.

By default this process stores the uploaded file in the path defined in config file codegenerator.files_upload_path

Note: when using checkbox, or multipleSelect, the items are stored in the database as JSON string. Additionally, the items in the index or form views are displayed separated by the value provided in the delimiter property.

delimiter

Default: "; "

When generating a form with checkbox or a select menu that accepts multiple answers, we need either store the results in a foreign model or store the records in a string field. By default, the code generator will convert the multiple options that a user selected into a JSON string before the results are stored using a Eloquent-mutator method.

When the data is presented on the show and/or index views, the options are displayed separated by the value of the delimiter. Of course, you can always change this behavior to fit your needs by removing the accessor and mutator methods in the model and modifying the views accordingly.

css-class

Default: ""

You can add custom css class(es) to the html input. Any value is placed in this option will be appended to the field's class="..." property. Classes that are already set in the views will not be replaced.

date-format

Default: "m/d/Y H:i A"

Any field with the type date, time or datetime can be formatted different when it is displayed. You can change the display format using this option.

html-value

Default: null

A default value to set the field to.

When using multiple options based html-type like checkbox, multipleSelect you can set this property to array of values to set multiple values by default. Ex, ["Red","Green"]

options

Default = empty string If you used select, checkbox, or radio for the html-type property, this is where you provide the options. Here are some example of the schema

#Example 1, a simple array

In this option, the value will be the numeric index value of the item in the array.

"options": [
    "Prefer not to say",
    "Male",
    "Female"
]

#Example 2, explicit values

"options": {
    "": Prefer not to say",
    "male": "Male",
    "female": "Female"
}

#Example 3, multiple language phrases for each option

"options": {
    "en": {
        "": "Prefer not to say",
        "male": "Male",
        "female": "Female"
    },
    "ar": {
        "": "Prefer not to say in Arabic",
        "male": "Male in Arabic",
        "female": "Female in Arabic"
    },
    "fr": {
        "": "Prefer not to say in French",
        "male": "Male in French",
        "female": "Female in French"
    }
}
is-inline-options

Default = false

If the html-type is set to radio or checkbox, setting this option to true will put the items next to each other instead of a vertical list.

placeholder or place-holder

Default = empty string

You can set a placeholder value when html-type is set to text, number, email, textarea orselect.

is-on-index

Default = true

Setting the value to false will prevent from adding this field to the index view.

is-on-form

Default = true

Setting the value to false will prevent from adding this field to the form view.

is-on-show

Default = true

Setting the value to false will prevent from adding this field to the show view.

is-on-views

Default = true

Setting the value to false will prevent from adding this field to the index, form or show view. This is just a short way to change the visibility for all views.

is-header

Default = false

Only one field can be set to a header. The header field will be use as the page header in the show view.

The key common_header_patterns in the configuration file, allow you to list the commond field name to automaticly set them as header.

Database Properties
data-type

Default = varchar

The database column type. The following are valid types.

'char', 'date', 'datetime', 'datetimetz', 'biginteger', 'bigint', 'blob', 'binary', 'bool', 'boolean', 'decimal', 'double', 'enum', 'list', 'float', 'int', 'integer', 'ipaddress', 'json', 'jsonb', 'longtext', 'macaddress', 'mediuminteger', 'mediumint', 'mediumtext', 'morphs', 'string', 'varchar', 'nvarchar', 'text', 'time', 'timetz', 'tinyinteger', 'tinyint', 'timestamp', 'timestamptz', 'unsignedbiginteger', 'unsignedbigint', 'unsignedInteger', 'unsignedint', 'unsignedmediuminteger', 'unsignedmediumint', 'unsignedsmallinteger', 'unsignedsmallint', 'unsignedtinyinteger', 'uuid', 'uuid'

Note: you can add short cuts if needed to in the codegenerator.php config file. You can add new mapping to the eloquent_type_to_method array.

data-type-params

This option allows you to specify parameters for the data type. Please ensure you provide valid parameters otherwise unexpected behavior will occur. For example, varchar and char will only need a maximum of one integer parameter where double, decimal and float require two integer parameters.

Command line example with specifying a decimal precision and scale:

Command line example data-type-params=5,2

JSON file example
"data-type-params": [
    5,
    2
]
If this option left out while some sort of a string data-type was used along with a max validation rule, the max value is used to limit the length of the sting in the database when a migration is generated.
data-value

Default = null

The default value for the database column.

is-auto-increment

Default = false

Setting this value to true will make this column a primary with auto increment identity.

is-primary

Default = false

You can set this field as the primary for retrieving records from the database. You can only set one column as the primary. If you set multiple fields are primary, the first one will be selected and the rest will be ignored.

Note: if you set the is-auto-increment field, this option will automatically get set. Ths only time this can be used is to create a primary field you don't wish for the database to auto assign it.

is-index

Default = false

Setting this value to true will add index to this column.

is-unique

Default = false

Setting this value to true will add a unique index to this column.

is-nullable

Default = false

Setting this value to true will make this column nullable.

Note: when setting this option to true, the default value will be set to NULL unless you pass a different value to data-value

When the validation rule contains "nullable", "required_if", "required_unless", "required_with", "required_with_all", "required_without", "required_without_all" or does NOT contains "required" rule, this flag will automatically gets set.
is-unsigned

Default = false

Setting this value to true will make this column unsigned. This option should only be used with numeric types only.

comment

This option will allow you to add meta description of the field in the database.

is-data

This option will allow you to casts a data filed to a Carbon object.

cast-as

This option will allow you to cast a field to php's native type.

foreign-relation

This option will allow you to create a foreign relation between the models.

{
    "name": "creator",    // the name of the relation
    "type": "belongsTo",  // the type of the relation
    "params": [           // the parameters for the used relation.
        "App\\User",
        "created_by"
    ],
    "field": "name"      // the name of the field on the foreign model to use as display value
}
foreign-constraint

This option will allow you to create a foreign relation between the models.

{
    "field": "user_id",                     // the field name.
    "references": "id",                     // the field to reference on the foreign model.
    "on": "users",                          // the foreign model begin referenced.
    "on-delete":"cascade",                  // the on-delete action.
    "on-update":"cascade",                  // the on-update actions.
    "references-model": "App\\Models\\User" // the namespace of the foreign model.
}
on-store

This option allows you to set a fixed value on the store action. For example, Illuminate\Support\Facades\Auth::Id(); will set the value to the current user id when the model is first created. Assuming you're using Laravel Authentication.

on-update

Similar to on-storeThis option allows you to set a fixed value on the update action.

Managing fields using JSON file

Storing the field's specification in a JSON file enables you to easily reuse the field with multiple commands. It also allows you to recreate the resources in the future if you decided to add/remove fields after the views have been crafted. The JSON files are typically stored in /resources/laravel-generator. If you don’t like where these files are kept, you can change that path from the config/laravelgenerator.php file.

The following command should be used to manage the resource-file to make this process easier.

  • php artisan resource-file:from-database [model-name]
  • php artisan resource-file:create [model-name]
  • php artisan resource-file:append [model-name]
  • php artisan resource-file:reduce [model-name]
  • php artisan resource-file:delete [model-name]

Resources mapping file

The resources-map file, is a JSON file that is used to keep track of the fields-file and the model classes to allow you to create the resources all at once.

The default file name is resources_map.json and can be changed from the configuration file.

When using resource-file:create, resource-file:from-database or resource-file:delete commands, a file called resources_map.json is automaticly updated.

The following is the structure of the file.

 {
    {
        "model-name": "Brand",
        "resource-file": "brands.json"
    },
    {
        "model-name": "Customer",
        "resource-file": "customers.json",
        "table-name": "customers_table"
    }
}

All option that are available to the create:resources can be used in the mapping file to make creating resources for all models customizable. Here is an example

 {
    {
        "model-name": "Customer",
        "resource-file": "customers.json",
        "table-name": "customers_table",
        "routes-prefix" "customers_prefix"
    }
}

To generate all the resources mapped in the resources_map.json file, use the following command

php artisan create:mapped-resources [model-name]

Generating clean and complete fields out of the box!

When using the commands that generate fields, our goal is to generate fields configured and ready for use without having to make any change to the generated fields.

While it is not possible to cover 100% of the use cases, Laravel-code-generator is shipped with a powerful configuration option to allow you to add conditions to handle your own use case.

The key common_definitions in the /config/codegenerator.php file allows you match field name using pattern then set the properties accordingly.

For example, you may want to add a global date, time, or datetime picker using javascript for any field where its name ends with _at.

You can do that by adding the following entry

[
    'match' => ['*_at'],
    'set'   => [
        'class'   => 'datetime-picker',
    ]
],

The same thing can be done for any field that ends with _date or starts wih date_of

[
    'match' => ['*_date','date_of_*'],
    'set'   => [
        'class'   => 'date-picker',
    ]
],

Of cource, you can set any of the field's option like html-type, data-type, data-type-params or foereign relation. You can set the configuration as fits your enviornmant, then you'll be able to create fields-file ready to generate resources with minimal work!

The conditions are applied to each field top to bottom, the configuration at the bottom of the array will take presence over the once on the top in case multiple conditions were matched.

It is strongly recomended to read the comments above each option in the configuration file to help you understand and customize the generator to fit your needs!

Foreign Relations

If you're using a code-first-approach and like to define relations between your models, you can easily define that in the relations keyword entry of the resource-file. Each relation can be defined using the following schema

{
    "name": "posts",            // the name of the relation
    "type": "hasMany",          // the type of the relation
    "params": [                 // the parameters for the used relation.
        "App\\Models\\Comment",
        "post_id",
        "id"
    ],
    "field": "name"             // the name of the field on the foreign model to use as display value
}

When creating hasOne() or belongsTo() relations, it be best to define them at the field level using the foreign-relation option.

Composite Indexes

If you're using a code-first-approach and like to define indexes with multiple columns, you can easily do that by adding these indexed to the Indexes keyword entry in the resource-file file. Each composite index can be defined using the following schema

{
    "name": "owner",  // The name of the index to use, if no name is set a one will be generated.
    "type": "unique", // Valid index type is one of the following 'index','unique' or 'primary'. If the type is not provided, 'index' is used.
    "columns": [      // List of the columns' names to be included in the index left to right.
        "first_name",
        "last_name"
    ]
}

Configurations

Overview

Laravel-Code-Generator ships with lots of configurable option to give you control of the generated code. It is strongly recommended that you read the comments block above each option in the config/codegenerator.php file to get familer with all available options.

Starting at version 2.2 it ships with a unique way to override/extend the default settings to prevent you from losing your setting when upgrading the package. The config/codegenerator_custom.php is a dedecated file to store your options. This file will always be controlled by you and will never be overriden by the package. To override any configuration found in config/codegenerator.php, simple add the same option in your custom file. The generator will look at the your configuration before falling back to the default config. Note, any array based option will be extended not overriden. For more info read the comment block in the config/codegenerator_custom.php

The most important option in the configuration file is common_definitions. This option allows you to set the default properties of new field using the name of that field. Your goal should be to generate 100% ready resource-file using this config. It will save you lots of time since all your fields will get generated using the desired properties. In another words, when using resource-file:create, resource-file:append or resource-file:from-database to create resource file, the generated JSON will be 100% ready for you without any manual modification.


Using Laravel-Collective to generate views

Overview

To use Laravel-Collective to generate view, you'll have to install the Laravel-Collective package.

Laravel-Code-Generator is capable of fully generating views using Laravel-Collective package. In fact, it is shipped with a template based on Laravel-collective called "default-collective".

By defaut, the template "default-collective" is not published to the resources folder as it is not needed out of the box. To publish it, use the command-line to execute the following command.

php artisan vendor:publish --provider="CrestApps\CodeGenerator\CodeGeneratorServiceProvider" --tag=default-collective

How to generate views using the Laravel-Collective package?

There are two ways to generate views using Laravel-Collective

Via the package configuration

Open the config file of the package /config/codegenerator.php change the value of the key template to default-collective

Or, via command-line

Change the template name at run time. In another words, pass the following option--template-name=default-collective from command-line

How to create a new template based on Laravel-Collective?

First, duplicate the folder /resources/codegenerator-templates/default-collective and name it anything your like.

Second, open up the package config file and add the new template name to the laravel_collective_templates array.