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 it’s making a model, controller view, etc.. and many other files that you would normally create in a Laravel project.

Although artisan commands can help create 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.

Let’s Start by Installing Blueprint

Note! For this package to work, your Laravel installation version should be at 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 your 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 a 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 a command.

By default, the 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 the blueprint will generate all of these 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 Generated Files

You can also revert 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.

Asmit Nepali, a Full Stack Developer, holds a Software Engineering degree from Gandaki College of Engineering and Science. Proficient in PHP Laravel, Vue.js, MySQL, Tailwind, Figma, and Git, he possesses a robust technical skill set.

Leave a Comment