This documentation belongs to an older version of the Laravel-Code-Generator package.

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 full resources using a single command with/without 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 field in a json file and recreate resources when the business needs changes.
  • Create standard CRUD controllers with simple or form-request validation.
  • Create model with relations.
  • Create named routes with and without group.
  • Create standard CRUD views.
  • Very flexible and rich with configurable options.
  • Easy commands to create fields-file; additionally, add or reduce existing fields-file.
  • Intelligent enough to automatically handles the relations between the models.
  • 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.
  • Customizable view’s templates to enable you to change the standard look and feel of your application.
  • 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.
  • Create a very clean and reusable code.

Live Demo

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

This demo was generated using the following two commands

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

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) (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 you design your own templates using a different or no css framework.

Available Commands

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

  • php artisan create:layout [application-name]
  • php artisan create:resources [model-name]
  • php artisan create:mapped-resources
  • php artisan create:controller [model-name]
  • php artisan create:model [model-name]
  • php artisan create:routes [model-name]
  • php artisan create:views [model-name]
  • php artisan create:create-view [model-name]
  • php artisan create:edit-view [model-name]
  • php artisan create:index-view [model-name]
  • php artisan create:show-view [model-name]
  • php artisan create:form-view [model-name]
  • php artisan create:migration [model-name]
  • php artisan create:form-request [model-name]
  • php artisan create:language [model-name]
  • php artisan create:fields-file [model-name]
  • php artisan fields-file:create [model-name]
  • php artisan fields-file:append [model-name]
  • php artisan fields-file:reduce [model-name]
  • php artisan fields-file:delete [model-name]

Examples

Lets create a CRUD called AssetCategory with the fields listed below.

  • id
  • name
  • description
  • is_active

Basic example

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

The above command will create fields-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!

Basic example using translations for english and arabic

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

The above command will create fields-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!

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 fields-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 fields-file from existing database separately using create:fields-file AssetCategory --translation-for=en,ar


Prologue

Release Notes

Version 2.1 introduces more flexibilty, less work for you out of the box! Follow is a list of all new features and changes that were introduced.

 

Everything is translatable!

When generation a resources for a multiple-languages based application, Laravel-Code-Generator provides you with a translation to everything out of the box! That means in addition to the fields, all button, labels, placeholder and title will have a locale key.

More configurations to allow very minimal effort to generate 100% production ready code.

generic_view_labels was added to the configuration file to allow you to add translations for the generic labels like button and title with in the view. You can even add custom templates to automaticly replace!

placeholder_by_html_type was added to the configuration file to allow you to generate placeholder text by html-type when no placeholder is provided. This will save you from having to manually create placeholder text for your fields. If you don't like to use placeholder for text, you can easily remove `'text' => 'Enter [% field_name %] here...',` from the configuration.

Creating multiple resorces using one command!

Yes! You are now able to create multiple resources using a mapping file. In other words, you can create complete resources for your entire database using one command!

Easier commands

For the sake of consistency and to make the commands easier to use and remember, almost all commands now accept the same argument which is the model name. (ex, Asset, AssetCategory...)

You no longer need to add the fields file name as an option (ex. --fields-file:assets.json), it is automaticly passed. For example, when using php create:resources AssetCategory the generator will look for a file called assets_categories.json.

New commands

php artisan fields-file:delete allows you to delete existing fields file. Using this command also updates the resources mapping file.

php artisan create:mapped-resourced allows you to generate multiple resources all at once.

Multiple Bug fixed

All known bugs have been addressed!

Easier installation with Laravel 5.5!

When using Laravel >= 5.5, there is no need to update the config/app.php file to bootstrap the package! Laravel will take care of that for you using the composer.json file.

Upgrade Guide

  • Update your crestapps/laravel-code-generator dependency to 2.1.* in your composer.json file.
  • From the command line, execute the following two commands
    • composer update
    • php artisan vendor:publish --provider="CrestApps\CodeGenerator\CodeGeneratorServiceProvider" --tag=default --force
  • Since this package is only needed when using the command-line, It is best to only bootstrap it while using the command-line as this will increase your app performance. The following steps will explain how to change the bootstrapping level
    • Open the config/app.php file. Remove the CrestApps\CodeGenerator\CodeGeneratorServiceProvider::class, line from the providers array.
    • (Skip this step when using Laravel >= 5.5) 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');
      }


How To...


All examples below assumes you already created a filds-file resources/codegenerator-fields/posts.json. The file can be created using the following command php artisan fields-file:create Post --name=id,title,details,is_active

How to create "views-layout"?

Command
php artisan create:views-layout [application-name]
Or
php artisan create:layout [application-name]
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.

--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]
Example
php artisan create:resources Post
model-name

The model name to create resources for.

--fields-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 fields from. This option allows you have all fields 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 fields-file can be found in the "Managing fields using JSON file" section.

--fields

Fields to use for creating the validation rules. More documentation about this option can be found in the "assign model fields from JSON file" section below.

This option will be deprecated in future releases. It is recommended to use the --fields-file option instead.

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

Prefix of the route group.

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

--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, a fields' file will be automatically generated. The generated file name will be names exactly like the database's table name. 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. 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.

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

This option will only work when using --table-exists option.
--lang-file-name

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 code message will be added.

This option will only work when using --table-exists option.
--fillable

A comma separated string to put in the fillable property of the model. For example 'title','description'

You may also set the fillable fields from the fields property directly. When this option is left out, any field that is flagged is-on-form will automatically be added to the fillable list.

This option will be deprecated in future releases. Instead, set the property is-on-form to true in the fields-file.
--relationships

A string to define the relationships to other models. This option allows you to create relationship between different models. For example, to build a has-many relation between "Post" model and "Comment" model the following string is used "comments#hasMany#App\Models\Comment|id|post_id"

Here is a description of the string read from left to right using the # as a separator

comments the relationship name "aka the method name"

hasMany the type of relation.

App\Models\Comment first parameter to pass to hasMay method.

Id second parameter to pass to hasMay method.

post_id third parameter to pass to hasMay method.

For more info about the available relations and the required parameters, visit Laravel documentation

This option will be deprecated in future releases. Instead, use the property foreign-relation in the fields-file to set the relation.
--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.

--indexes

A list of index can be provide here. You may wish to create composite index. Additionally, you can set the single field to be an indexes from the fields setup. This can be done by setting the is-index, is-unique, or is-primary properties of a field.

Here is an example, --index='update_at','name'#unique='some_unique_column'#unique=col1_name,col2_name

--foreign-keys

You can provide a list of foreign keys to add.

Here is an example, --foreign-keys=user_id|id|users|cascade|cascade#deleted_by|id|users

This example will generate the following code

$table->foreign('user_id')
	  ->references('id')
	  ->on('users')
	  ->onDelete('cascade')
	  ->onUpdate ('cascade');

$table->foreign('deleted_by')
	  ->references('id')
	  ->on('users');
This option will be deprecated in future releases. Instead, use the property foreign-constraint in the fields-file to set the relation.
--without-migration

When using a database first approach, migrations are not necessary. Providing this option will prevent the resources command from creating a migration file.

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

--force

This option will override any existing files if any exists.

How to create multiple resources at once?

When using fields-file:create, create:fields-file and fields-file:delete updates the resources_map.json file. This option can be used to generate resources for all the mapped resources.

Command
php artisan create:mapped-resources
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, a fields' file will be automatically generated. The generated file name will be names exactly like the database's table name. 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. 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.

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

This option will only work when using --table-exists option.
--lang-file-name

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

--without-migration

When using a database first approach, migrations are not necessary. Providing this option will prevent the resources command from creating a migration file.

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

--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 file will be used.

How to create a controller?

Command
php artisan create:controller [model-name]
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".

--fields-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 fields from. This option allows you have all fields 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 fields-file can be found in the "Managing fields using JSON file" section.

--fields

Fields to use for creating the validation rules. More documentation about this option can be found in the "assign model fields from JSON file" section below.

This option will be deprecated in future releases. It is recommended to use the --fields-file option instead.

--routes-prefix

Prefix of the route group.

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

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

--lang-file-name

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

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

--force

This option will override the controller if one already exists.

How to create a model?

Command
php artisan create:model [model-name]
Example
php artisan create:model Post
model-name

The name of the model.

--fields-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 fields from. This option allows you have all fields 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 fields-file can be found in the "Managing fields using JSON file" section.

--fields

Fields to use for creating the validation rules. More documentation about this option can be found in the "assign model fields from JSON file" section below.

This option will be deprecated in future releases. It is recommended to use the --fields-file option instead.

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

--fillable

A comma separated string to put in the fillable property of the model. For example 'title','description'

You may also set the fillable fields from the fields property directly. When this option is left out, any field that is flagged is-on-form will automatically be added to the fillable list.

This option will be deprecated in future releases. Instead, set the property is-on-form to true in the fields-file.
--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

--relationships

A string to define the relationships to other models. This option allows you to create relationship between different models. For example, to build a has-many relation between "Post" model and "Comment" model the following string is used "comments#hasMany#App\Models\Comment|id|post_id"

Here is a description of the string read from left to right using the # as a separator

comments the relationship name "aka the method name"

hasMany the type of relation.

App\Models\Comment first parameter to pass to hasMay method.

Id second parameter to pass to hasMay method.

post_id third parameter to pass to hasMay method.

For more info about the available relations and the required parameters, visit Laravel documentation

This option will be deprecated in future releases. Instead, use the property foreign-relation in the fields-file to set the relation.
--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.

--force

This option will override the model if one already exists.

How to create routes?

Command
php artisan create:routes [model-name]
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

Prefix of the route group.

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

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

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]
Example
php artisan create:views Post
model-name

The model name that the created views will represent.

--fields-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 fields from. This option allows you have all fields 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 fields-file can be found in the "Managing fields using JSON file" section.

--fields

Fields to use for creating the validation rules. More documentation about this option can be found in the "assign model fields from JSON file" section below.

This option will be deprecated in future releases. It is recommended to use the --fields-file option instead.

--routes-prefix

Prefix of the route group.

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

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

--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]
Example
php artisan create:create-view Post
model-name

The model name that this view will represent.

--fields-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 fields from. This option allows you have all fields 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 fields-file can be found in the "Managing fields using JSON file" section.

--fields

Fields to use for creating the validation rules. More documentation about this option can be found in the "assign model fields from JSON file" section below.

This option will be deprecated in future releases. It is recommended to use the --fields-file option instead.

--routes-prefix

Prefix of the route group.

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

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

--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]
Example
php artisan create:edit-view Post
model-name

The model name that this view will represent.

--fields-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 fields from. This option allows you have all fields 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 fields-file can be found in the "Managing fields using JSON file" section.

--fields

Fields to use for creating the validation rules. More documentation about this option can be found in the "assign model fields from JSON file" section below.

This option will be deprecated in future releases. It is recommended to use the --fields-file option instead.

--routes-prefix

Prefix of the route group.

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

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

--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]
Example
php artisan create:index-view Post
model-name

The model name that this view will represent.

--fields-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 fields from. This option allows you have all fields 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 fields-file can be found in the "Managing fields using JSON file" section.

--fields

Fields to use for creating the validation rules. More documentation about this option can be found in the "assign model fields from JSON file" section below.

This option will be deprecated in future releases. It is recommended to use the --fields-file option instead.

--routes-prefix

Prefix of the route group.

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

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

--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]
Example
php artisan create:show-view Post
model-name

The model name that this view will represent.

--fields-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 fields from. This option allows you have all fields 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 fields-file can be found in the "Managing fields using JSON file" section.

--fields

Fields to use for creating the validation rules. More documentation about this option can be found in the "assign model fields from JSON file" section below.

This option will be deprecated in future releases. It is recommended to use the --fields-file option instead.

--routes-prefix

Prefix of the route group.

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

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

--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]
Example
php artisan create:form-view Post
model-name

The model name that this view will represent.

--fields-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 fields from. This option allows you have all fields 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 fields-file can be found in the "Managing fields using JSON file" section.

--fields

Fields to use for creating the validation rules. More documentation about this option can be found in the "assign model fields from JSON file" section below.

This option will be deprecated in future releases. It is recommended to use the --fields-file option instead.

--routes-prefix

Prefix of the route group.

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

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

--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]
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".

--fields-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 fields from. This option allows you have all fields 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 fields-file can be found in the "Managing fields using JSON file" section.

--fields

Fields to use for creating the validation rules. More documentation about this option can be found in the "assign model fields from JSON file" section below.

This option will be deprecated in future releases. It is recommended to use the --fields-file option instead.

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

--indexes

You can provide a list of indexes here. However, you can also set the single column indexes from the fields. This can be done by setting the is-index, is-unique, or is-primary properties of a field.

Here is an example, --index='update_at','name'#unique='some_unique_column'#unique=col1_name,col2_name

--foreign-keys

You can provide a list of foreign keys to add.

Here is an example, --foreign-keys=user_id|id|users|cascade|cascade#deleted_by|id|users

This example will generate the following code

$table->foreign('user_id')
	  ->references('id')
	  ->on('users')
	  ->onDelete('cascade')
	  ->onUpdate ('cascade');

$table->foreign('deleted_by')
	  ->references('id')
	  ->on('users');
This option will be deprecated in future releases. Instead, use the property foreign-constraint in the fields-file to set the relation.
--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.

--force

This option will override the migration if one already exists.

How to create form-request?

Command
php artisan create:form-request [model-name]
Example
php artisan create:form-request Post
model-name

The name of the model.

--class-name

The name of the form-request class.

--fields-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 fields from. This option allows you have all fields 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 fields-file can be found in the "Managing fields using JSON file" section.

--fields

Fields to use for creating the validation rules. More documentation about this option can be found in the "assign model fields from JSON file" section below.

This option will be deprecated in future releases. It is recommended to use the --fields-file option instead.

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

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

--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]
Example
php artisan create:language Post
model-name

The name of the model that these fields will represent.

--language-file-name

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

--fields-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 fields from. This option allows you have all fields 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 fields-file can be found in the "Managing fields using JSON file" section.

--fields

Fields to use for creating the validation rules. More documentation about this option can be found in the "assign model fields from JSON file" section below.

This option will be deprecated in future releases. It is recommended to use the --fields-file option instead.

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

How to create fields-file?

Command
php artisan fields-file:create [model-name]
Example
php artisan fields-file:create Post
model-name

The name of the model that these fields will represent.

--file-name

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.

--names

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

--data-types

A list of the data type to be created. The types should be separated by a comma.

--html-types

A list of the html type to be created. The types should be separated by a comma.

--without-primary-key

By default, this command will add primary key field called id to the resource. This option can be used to create the fields file without a primary key

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

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 field to existing fields-file?

Command
php artisan fields-file:append [model-name]
Example
php artisan fields-file:append Post
model-name

The name of the model that these fields will represent.

--file-name

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.

--names

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

--data-types

A list of the data type to be created. The types should be separated by a comma.

--html-types

A list of the html type to be created. The types should be separated by a comma.

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

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 field to existing fields-file?

When all fields are reduces, the file itself will be deleted.

Command
php artisan fields-file:reduce [model-name]
Example
php artisan fields-file:reduce Post
model-name

The name of the model that these fields will represent.

--file-name

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.

--names

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

How to delete existing fields-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.

Command
php artisan fields-file:delete [model-name]
Example
php artisan fields-file:delete Post
model-name

The name of the model that these fields represents.

--file-name

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 fields' 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 fields' 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 create:fields-file [table-name]
Example
php artisan create:fields-file posts
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.

--fields-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 fields from. This option allows you have all fields 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 fields-file can be found in the "Managing fields using JSON file" section.

--fields

Fields to use for creating the validation rules. More documentation about this option can be found in the "assign model fields from JSON file" section below.

This option will be deprecated in future releases. It is recommended to use the --fields-file option instead.

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

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.



Fields

Overview

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

General Properties
name

Required

A unique name for the field

label

A title to describe the field. If this option is left out, the field name is used to generate an English 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

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

css-class

Default: ""

You can add custom css class(es) to the html input. Any value is placed in this option will be added to the field's class="..." property.

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` for the `html-type` property, this is where you provide the options. Here are example of how to use it options=Male|Femle

Or if you want to have a different value that the title, you can pass the options like so options=1:Male|2:Female

However, when using --fields-file option to import json, you can define multiple language phrases for each option

"options": {
    "en": {
        "": "Prefer not to say",
        "1": "Male",
        "2": "Female"
    },
    "ar": {
        "": "Prefer not to say in Arabic",
        "1": "Male in Arabic",
        "2": "Female in Arabic"
    },
    "fr": {
        "": "Prefer not to say in French",
        "1": "Male in French",
        "2": "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 fields-file to make this process easier.

  • php artisan create:fields-file [model-name]
  • php artisan fields-file:create [model-name]
  • php artisan fields-file:append [model-name]
  • php artisan fields-file:reduce [model-name]
  • php artisan fields-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 fields-file:create, create:fields-file or fields-file:delete commands, a file called resources_map.json is automaticly updated.

The following is the structure of the file.

 {
    "1": {
        "model-name": "Brand",
        "fields-file": "brands.json"
    },
    "2": {
        "model-name": "Customer",
        "fields-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

 {
    "1": {
        "model-name": "Customer",
        "fields-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!

Creating fields from a raw string

Creating fields from a raw string will be deprecated in future releases. It is recomended that you use fields-file instead.

The code generator allows you to assign fields from a raw string. The following is an example of raw string.

--fields="name=title;is-on-show;is-on-form;is-on-index=false;label=Some title without translations#name=title;labels=en:Some description in English|fn:Some description in French;options=Test|1:Male|2:Female"

Any property that starts with the keyword "is" expected to be a boolean value (true|false). The value of that property will optional as the default value will be set to `true`.

Note: The fields are separated by a hash-tag `#`. Each property that belongs to a field is separated by semi-colon `;` Here is example of a valid string.

As shown in the example above, adding translation to the labels can easily be done. You can add a colon : before the label, and add list of languages separated by a bar |. To control the translation more efficiently, it is recommended use --fields-file option instead to provide a json formatted string.


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 from default 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.