Laravel Craftsman CLI


April 12th, 2019


Laravel Craftsman is a CLI for easily crafting Laravel assets for any project (artisan make on steroids) by Mike Erickson. This project relies on Laravel Zero, providing a suite of “project agnostic” CLI tools that you install globally:

1composer global require codedungeon/laravel-craftsman

You can create all assets for a typical model/view/controller setup using the craft:all command:

1laravel-craftsman craft:all Post \
2 --model App/Models/Post \
3 --tablename posts \
4 --rows 50 \
5 --extends \
6 --section content

Take note that this command also takes care of extending the layouts/app.blade.php file in template files.

Here’s an example from the readme you can use to generate a migration file complete with the table definition using the --fields option:

1--fields fname:string@25:nullable,lname:string@50:nullable,email:string@80:nullable:unique,dob:datetime,notes:text,deleted_at:timezone
3# results in...
4Schema::create('contacts', function (Blueprint $table) {
5 $table->bigIncrements('id');
6 $table->timestamps();
7 $table->string('fname', 25)->nullable();
8 $table->string('lname', 50)->nullable();
9 $table->string('email', 80)->nullable()->unique();
10 $table->datetime('dob');
11 $table->text('notes');
12 $table->timezone('deleted_at');

The project also allows you to configure the templates used for the generated files with the Mustache templating language. Here’s an example of the class template at the time of writing:

3namespace {{namespace}};
5class {{model}}
7 {{#constructor}}
8 function __construct() {}
9 {{/constructor}}

Check out the readme for the full list of commands and flags. You can also run laravel-craftsman list from the command line after you’ve installed the package globally and added it to your path. You can learn more about this package and check out the source code on GitHub at mikeerickson/laravel-craftsman.

Filed in:

Paul Redmond

Full stack web developer. Author of Lumen Programming Guide and Docker for PHP Developers.