Learning Laravel Blueprint - A Rapid Component Development Package

Learning Laravel Blueprint - A Rapid Component Development Package

Posted on:April 20, 2020 at 10:00 AM

The BluePrint is a package for Laravel to develop component rapidly. The meaning of “Rapid” even you can develop the project without writing code. Incredible, right? This package is maintained by Laravel Shift.

According to the docs-

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

Let’s dig into it.

Table of Contents

Open Table of Contents

Requirements

  • Need to have Composer
  • Laravel Project.

How to Install?

Run the following command-

composer require --dev laravel-shift/blueprint

Blueprint commands

Once you installed, you will multiple commands in php artisan for blueprint.

For example-

blueprint
  blueprint:build      Build components from a Blueprint draft
  blueprint:erase      Erase components created from last Blueprint build
  blueprint:trace      Create definitions for existing models to reference in new drafts

First, let’s create a draft.yml file in our root directory of the project.

My Requirements

Imagine, I want to build a blog where I need to have-

  • A Post model
    • title that should be a string.
    • description that should be a long text.
    • user_id that should be a user id.
    • published_at that should be a boolean.
  • A PostController
    • index()
    • show()
    • create()
    • store()

Defining expected components

Now, define the required components in draft.yaml file like so-

models:
  Post:
    title: string
    description: longtext
    user_id: id
    publised: boolean
    published_at: nullable timestamp

Controllers:
  Post:
    index:
      query: all
      render: post.index with:posts
    create:
      render: post.create
    store:
      validate: title, description
      save: post
      redirect: post.index
    show:
      render: post.show with:post

So, you can define your requirement as a human-readable way in the ymal file. Isn’t that enough readable?

Now, run this command-

php artisan blueprint:build

It will generate the following files-

Created:
- database/migrations/2020_04_20_101727_create_posts_table.php
- app/Post.php
- database/factories/PostFactory.php
- app/Http/Controllers/PostController.php
- app/Http/Requests/PostStoreRequest.php
- resources/views/post/index.blade.php
- resources/views/post/show.blade.php
- tests/Feature/Http/Controllers/PostControllerTest.php

It’s basically created as we described in our draft.yaml file. Cool, isn’t so?

Even, it created relationships in models. For example, our case, a post basically belongs to a user. There should a relationship between user and posts. Here is our Post.php model. Even it’s created test for us PostControllerTest.php. Cool, right?

In our web.php file, there is a resource for post.

Even it creates some dummy view page in resources/views/post folder. Now you can adjust based on your expectation.

It’s cool enough, right?

Surely it is, however, still you have to adjust something in the code. For example, you want to use flash message once the data store.

in your draft.yaml file-

store:
      validate: title, description
      save: post
      flash: post.title
      redirect: post.index

Now, if we look into the store() method in the controller, we can see the flash message has defined as tile only. Obviously we need to update like “Post has been stored”. We need to do it manually.

public function store(PostStoreRequest $request)
{
    $post = Post::create($request->all());

    $request->session()->flash('post.title', $post->title);

    return redirect()->route('post.index');
}

The main purpose of this package to make your development faster, even beyond our thinking. And surely, they did it. A big clap for the development team.

And of course, there are lot more commands available. Check it out in the official repo: https://github.com/laravel-shift/blueprint

Alert: Just want to remind you that, if you don’t have knowledge of basic on Laravel, I strongly discourage you not to use this package because it may destroy your learning power. To use this package, you should be at least an intermediate level of developer.

Thank you.