Generate Laravel Code Using Artisan Command – Short Blueprint Guide

When developing a laravel project, developers often find themselves writing and repeating the same trivial codes. Whether if it’s making a model, controller or view, etc.. and many other files that you would normally create in a laravel project.

Although, artisan commands can be helpful creating files and components for your project, these are mostly just blank classes that you need to fill in with your logic.

In comes blueprint, this package lets you generate laravel codes a lot faster using a draft file. Here, you can define models and controllers with shorthand YAML syntax making it easy to build new components in your laravel application.

Blueprint is an open-source tool for rapidly generating multiple Laravel components from a single, human readable definition.

Lets start by installing Blueprint

Note! For this package to work, your laravel installation version should be least version 6 or higher.

Install Blueprint using this command:

composer require --dev laravel-shift/blueprint

Blueprint automatically discovers the package. Therefore, you don’t need to register you application in AppServiceProvider.php.

Defining Components in Yaml file

First, create a draft.yml in the root of your laravel application directory.

You can easily do this using blueprint:new command.

php artisan blueprint:new

This will create a new draft.yml file with sections for where to put the models and controllers.

For instance, here’s a sample of syntax to generate components for blog website.

    title: string:400
    content: longtext
    published_at: nullable timestamp
    author_id: id:user

      query: all
      render: post.index with:posts

      validate: title, content, author_id
      save: post
      send: ReviewPost with:post
      dispatch: SyncMedia with:post
      fire: NewPost with:post
      flash: post.title
      redirect: post.index

Generating Components Defined in Yaml file

After that, you can simply generate all of this using blueprint:build command.

By default, blueprint will attempt to load draft.yml from the root of the project. But you can also specify the path draft.yml file by passing a single argument blueprint:build [draft].

php artisan blueprint:build

Next, we will execute the command and blueprint will generate all of this files and even update routes/web.php.

- database/migrations/2020_07_21_054321_create_posts_table.php
- app/Post.php
- database/factories/PostFactory.php
- app/Http/Controllers/PostController.php
- app/Events/NewPost.php
- app/Http/Requests/PostStoreRequest.php
- app/Jobs/SyncMedia.php
- app/Mail/ReviewPost.php
- resources/views/post\index.blade.php
- tests/Feature/Http/Controllers/PostControllerTest.php       

- routes/web.php

Revert Back Generated Files

You can also revert back if you made mistakes anytime using blueprint:erase command.

php artisan blueprint:erase

This command will undo changes in routes and delete the list of files generated last time using blueprint:build command.

If you are interested you can learn more about this package in blueprint documentation and view their source code in laravel-shift/blueprint.

Leave a Comment