Skip to content
Laravel Craftsman CLI for easliy crafting Laravel assets for any project (artisan make on steroids)
Branch: master
Clone or download
Latest commit f5cf449 Apr 22, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.github/ISSUE_TEMPLATE Update issue templates Apr 20, 2019
app chore: fixed some regression issues Apr 22, 2019
bootstrap initial commit Mar 19, 2019
builds
config chore: add resource controller Apr 19, 2019
docs/images chore: fixed some regression issues Apr 22, 2019
tasks chore: wip Apr 20, 2019
templates chore: add resource controller Apr 19, 2019
tests chore: wip Apr 20, 2019
.editorconfig chore: preparing for deployment Mar 27, 2019
.gitattributes initial commit Mar 19, 2019
.gitignore chore: add resource controller Apr 19, 2019
.prettierrc
.travis.yml chore: preparing for 1.0.6 release Apr 2, 2019
CHANGELOG.md chore: wip Apr 20, 2019
LICENSE chore: scaffold the initial commands Mar 19, 2019
README.md chore: wip Apr 20, 2019
box.json chore: preparing for deployment Mar 27, 2019
commitlint.config.js chore: code update Mar 19, 2019
composer.json chore: wip Apr 20, 2019
composer.lock
config.php refactor: complete refactor of tests Apr 6, 2019
laravel-craftsman initial commit Mar 19, 2019
laravel-craftsman.png
package-lock.json chore: code update Mar 19, 2019
package.json chore: build 1.2.1 Apr 22, 2019
phpunit-printer.yml chore: wip Apr 20, 2019
phpunit.ci.xml chore: wip Apr 20, 2019
phpunit.xml chore: wip Apr 20, 2019

README.md

Laravel Craftsman

Description

Laravel Craftsman (written using the awesome Laravel-Zero CLI builder) provides a suite of crafting assets using a project agnostic CLI.

You can quickly create class, controller, factory, migration, model, seed and view assets. In addition, you can create all assets with a single command, allowing you to quickly craft a new resource in seconds!

📝 Laravel News Article

📦 Packagist

Laravel Craftsman

Installation

Using Composer

> composer global require codedungeon/laravel-craftsman

Using curl/wget

> curl -o laravel-craftsman https://github.com/mikeerickson/laravel-craftsman/archive/master.zip

or

> wget https://github.com/mikeerickson/laravel-craftsman/archive/master.zip

Usage

> laravel-craftsman <command> [options] [arguments]

> laravel-craftsman craft:all Post --model App/Models/Post --tablename posts --rows 50 --extends layouts.app --section content

> laravel-craftsman craft:class App/TestClass --constructor

> laravel-craftsman craft:controller PostController --model App/Models/Post

> laravel-craftsman craft:factory PostFactory --model App/Models/Post

> laravel-craftsman craft:migration create_posts_table --model App/Models/Post --tablename posts

> laravel-craftsman craft:model App/Models/Post --tablename posts

> laravel-craftsman craft:seed PostTableSeeder --model App/Models/Post --rows 100

> laravel-craftsman craft:views authors --extends partials.master --section content

Commands

The following commands are available in any Laravel project. You can use the individual crafting routines which are similar to the Artisan commands, but the craft:all command is the most powerful of the bunch.

Using craft:all you can easily generate all assets (controller, factory, migration, model, and seed) for a given resource (ie Post, Customer, etc)

laravel-craftsman craft:all Contact \
  --model App/Models/Contact \
  --tablename contacts \
  --rows 50 \
  --fields first_name:string@30:nullable,last_name:string@50:nullable,email:string@80:nullable:unique
Command Name / Option Description
craft:all base name Creates all assets (Controller, Factory, Migration, Model, Seed)
--model, -m Path to model (eg App/Models/Post)
--tablename, -t Tablename used in database (will set $tablename in Model)
If not supplied, default table will be pluralized model name
--rows, -r Number of rows used by seed when using Factory
--extends, -x View extends block (optional)
--section, -i View section block (optional)
--no-controller, -c Do not create controller
--no-factory, -a Do not create factory
--no-migration, -g Do not create migration
--no-model, -o Do not create model
--no-seed, -s Do not create seed
--no-views, -e Do not create seed
craft:class class name Creates empty class
--constructor, -c Include constructor method
--template, -t Path to custom template (override config file)
--overwrite, -w Overwrite existing class
craft:controller controller name Create controller using supplied options
--model, -m Path to model (eg App/Models/Post)
--validation, -l Create validation blocks where appropriate
--api, -a Create API controller (skips create and update methods)
--empty, -e Create empty controller
--template, -t Path to custom template (override config file)
--overwrite, -w Overwrite existing class
craft:factory factory name Creates factory using supplied options
--model, -m Path to model (eg App/Models/Post)
craft:migration migration name Creates migration using supplied options
--model, -m Path to model (eg App/Models/Post)
--tablename, -t Tablename used in database (will set $tablename in Model)
If not supplied, default table will be pluralized model name
--fields, -f List of fields (option) see syntax below
🚨 If you have spaces separating fields, you must surround
fields list in quotes
--down, -d Include down methods (skipped by default)
--template, -t Path to custom template (override config file)
--overwrite, -w Overwrite existing class
craft:model model name Creates model using supplied options
See below about defining alternate model path
--tablename, -t Tablename used in database (will set $tablename in Model)
If not supplied, default table will be pluralized model name
--template, -m Path to custom template (override config file)
--overwrite, -w Overwrite existing class
craft:seed base seed name Creates seed file using supplied options
--model, -m Path to model (eg App/Models/Post)
--rows, -r Number of rows to use in factory call (Optional)
--template, -t Path to custom template (override config file)
--overwrite, -w Overwrite existing class
craft:views base resource Creates view files
--extends, -x Includes extends block using supplied layout
--section, -s Includes section block using supplied name
--no-create, -c Exclude create view
--no-edit, -d Exclude edit view
--no-index, -i Exclude index view
--no-show, -w Exclude show view

Defining Class Path

When crafting resources which are not automatically created in their assigned directories, you can define the location to the path where asset is created as follows:

> laravel-craftsman craft:class App/Services/Sync ...

This will create a class in the App/Services path, with filename Sync.php. Directories (including nested directories) will be created if they do not already exists.

Supported Commands:

The following commands support defining class path

  • craft:class
  • craft:factory
  • craft:model
  • craft:seed
  • craft:test

Single Use Template

In addition to the standard templates, you may also define a single use template which is only used during command execution. Single use templates are designed to reference project specific templates, and you use the <projet> keyword when executing the desire command.

> laravel-craftsman craft:class App/Services/SyncService --template "<project>/templates/service.mustache" ...
oh-my-zsh Conflict

If you have oh-my-zsh installed, make sure you wrap template value in quotes, otherwise you may receive an error

laravel-craftsman craft:class TestService --template <project>/templates/custom.mustache --overwrite
zsh: no such file or directory: project

Field Option Syntax

When using the --fields option when building migrations, you should use the following syntax:

format:
fieldName:fieldType@fieldSize:option1:option2:option3

example:
email:string@80:nullable:unique

--fields fname:string@25:nullable,lname:string@50:nullable,email:string@80:nullable:unique,dob:datetime,notes:text,deleted_at:timezone

    Schema::create('contacts', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->timestamps();
        $table->string('fname', 25)->nullable();
        $table->string('lname', 50)->nullable();
        $table->string('email', 80)->nullable()->unique();
        $table->datetime('dob');
        $table->text('notes');
        $table->timezone('deleted_at');
    });

Tips

Boolean Option Shortcut

When executing any of the laravel-craftsman commands, if you wish to apply one or more switches (those options which do not require a corresponding value), you can use the standard CLI shorthands (this tip can be used in any CLI based tool, not just laravel-craftsman (well assuming the CLI actually supports shorthand).

For example:

Lets assume you wish to wish to create views, you can use the following command to skip creation of the create (-c), edit (-d) and show (-w) views (only creating the index view). The combination of -cdw is shorthand for --no-create --no-edit --no-show

> laravel-craftsman craft:views --extends layouts.app --section content -cdw

is same as

> laravel-craftsman craft:views --extends layouts.app --section content --no-create --no-edit --no-show

> laravel-craftsman craft:views --extends layouts.app --section content -c -d -w

Defining Nested Paths

Any command can store assets within tested folders within default path by separating name argument with forward slash For example, the following command will define the path for model asset in the App/Models/<name> path

> laravel-craftsman App/Models/Customer ...

Custom Templates

Laravel Craftsman provides support for creating custom templates if you wish to change the syntax to match your personal style. The default templates use the standard Laravel syntax, but we like to allow ou have your own flair.

User Customized Templates

If you wish to create derivatives of the supported templates, you can customize the config.php located in the laravel-craftsman directory. By default, this will be ~/.composer/vendor/codedungeon/laravel-craftsman, but may be different depending on the method you chose to install laravel-craftsman.

    'templates' => [
            'class' => 'user_templates/class.mustache',
            'api-controller' => 'user_templates/api-controller.mustache',
            'empty-controller' => 'user_templates/empty-controller.mustache',
            'controller' => 'user_templates/controller.mustache',
            'factory' => 'user_templates/factory.mustache',
            'migration' => 'user_templates/migration.mustache',
            'model' => 'user_templates/model.mustache',
            'seed' => 'user_templates/seed.mustache',
            'view-create' => 'user_templates/view-create.mustache',
            'view-edit' => 'user_templates/view-edit.mustache',
            'view-index' => 'user_templates/view-index.mustache',
            'view-show' => 'user_templates/view-show.mustache',
        ],

Single Use Template

In addition to creating templates and configuring the config.php file, you may optionally supply a template to be used as single use (not stored) from all command execution For example, if you wish to create a standard class asset, you can use a single use template as follows:

> laravel-craftsman craft:class App/Services/Syncronize --template <project>/templates/service.mustache`


Template Variables

The following variables can be used in any of the supported templates (review the templates directory for a basis of how to create custom templates)

Variable Name Templates which variable is used
fields Used by migration
model Used by api-controller, class, controller, factory, migration, model and seed
model_path Used by api-controller, controller, factory, migration, seed
name Used by api-controller, controller and empty-controller
namespace Used by class, model
num_rows Used by seed
tablename Used by controller, migration, model
extends Used by views
section Used by views

License

Copyright © 2019 Mike Erickson Released under the MIT license

Credits

laravel-craftsman written by Mike Erickson

E-Mail: codedungeon@gmail.com

Twitter: @codedungeon

Website: codedungeon.io

You can’t perform that action at this time.