Rapid application scaffolding with Laravel Blueprint
Tags: webdev, laravel, tutorial
Published: 2022-06-02 17:42:09

Hello there,

In this tutorial we will see how to rapidly develop a Laravel application by utilising a code generation tool called Laravel Blueprint. 

Laravel Blueprint allows creating multiple Laravel components(Models, Controller,Event and others) from a single, human readable domain language.

## Blueprint features

#### Draft your application with a simple syntax

Blueprint uses a simple YAML syntax to define your models and controllers offering shorthands and leveraging conventions to maximize the developer experience.


#### Generate code using artisan commands

Blueprint comes with artisan commands making it easy and familiar to build new components and reference existing within your Laravel application.


#### Output multiple Laravel components at once

Blueprint not only generates models and controllers, but factories, migrations, form requests, events, jobs, and mailables. Oh, and tests too.

**To start**

## Step 1. Create new laravel application

```sh
laravel new blue-app
cd blue-app
```

## Step 2. Install `Blueprint` package

```sh
composer require --dev laravel-shift/blueprint
```

**Testing package for tests created**

```sh
composer require --dev jasonmccreary/laravel-test-assertions
```

**Ignoring Blueprint files**

```sh
echo '/draft.yaml' >> .gitignore
echo '/.blueprint' >> .gitignore
```

## Step 3. Scaffold Application using  YAML file

Create a draft.yaml file 
```sh
touch draft.yaml
```

Add the following

```yaml
models:
  Post:
    title: string:400
    content: longtext
    remark: string:100 nullable
    user_id: id foreign    
    published_at: nullable timestamp
    relationships:
      hasMany: Transaction      
      belongsTo: User
  Transaction:
    payment_token: string:40
    total: decimal:8,2
    user_id: id foreign
    post_id: id foreign:posts
    status: enum:pending,successful,failed
    relationships:      
      belongsTo: User, Post

seeders: Post, Comment


controllers:
  Post:
    index:
      query: all:posts
      render: post.index with:posts
    create:
      render: post.create
    store:
      validate: title, content, remark
      save: post
      send: ReviewNotification to:post.author with:post
      dispatch: SyncMedia with:post
      fire: NewPost with:post
      flash: post.title
      redirect: post.index
    show:
      render: post.show with:post
    edit:
      render: post.edit with:post
    update:
      validate: post
      update: post
      flash: post.id
      redirect: post.index
    destroy:
      delete: post
      redirect: post.index
  Transaction: 
    store:
      validate: transaction
      save: transaction
      flash: transaction.id
      redirect: post.index
    show:
      render: transaction.show with:transaction
  Report:
    invokable:
      fire: ReportGenerated
      render: report
```

**Files generated**

- A `model` class for Post, Transaction complete with fillable, casts, and dates properties, as well as relationships methods.
- A `migration` to create the posts and transaction tables.
- A `factory` intelligently set with fake data.
- A `controller` class for PostController with index and store actions complete with code generated for each statement.
- `Resource` routes in  `web.php` for the all controllers' actions.
- A `form request` of StorePostRequest validating title and content based on the Post model definition.
- A mailable class for ReviewNotification complete with a post property set through the constructor.
- A job class for SyncMedia complete with a post property set through the constructor.
- An event class for NewPost complete with a post property set through the constructor.
- A Blade template of post/index.blade.php rendered by PostController@index.
- An HTTP Test complete with respective arrange, act, and assert sections for the PostController actions.


At this point we have many files to check and review before moving forward. This tutorial ends here the next step is optional.


## Step 4. Scaffold Authentication (Optional)

Using the Breeze for Authentication code scaffolding. 


```sh
composer require laravel/breeze --dev
```


```sh
php artisan breeze:install
```


```sh
npm install && npm run dev
php artisan migrate
```

## Start the application

Start the development server

```sh
php artisan server
```

Access from the browser at http:://localhost:8000


## Happy Coding!