DEV Community

Alpha Olomi
Alpha Olomi

Posted on • Updated on

Rapid application scaffolding with Laravel Blueprint

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

laravel new blue-app
cd blue-app
Enter fullscreen mode Exit fullscreen mode

Step 2. Install Blueprint package

composer require --dev laravel-shift/blueprint
Enter fullscreen mode Exit fullscreen mode

Testing package for tests created

composer require --dev jasonmccreary/laravel-test-assertions
Enter fullscreen mode Exit fullscreen mode

Ignoring Blueprint files

echo '/draft.yaml' >> .gitignore
echo '/.blueprint' >> .gitignore
Enter fullscreen mode Exit fullscreen mode

Step 3. Scaffold Application using YAML file

Create a draft.yaml file

touch draft.yaml
Enter fullscreen mode Exit fullscreen mode

Add the following

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

seeders: Post, Comment

      query: all:posts
      render: post.index with:posts
      render: post.create
      validate: title, content, remark
      save: post
      send: ReviewNotification with:post
      dispatch: SyncMedia with:post
      fire: NewPost with:post
      flash: post.title
      redirect: post.index
      render: with:post
      render: post.edit with:post
      validate: post
      update: post
      redirect: post.index
      delete: post
      redirect: post.index
      validate: transaction
      save: transaction
      redirect: post.index
      render: with:transaction
      fire: ReportGenerated
      render: report
Enter fullscreen mode Exit fullscreen mode

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.

composer require laravel/breeze --dev
Enter fullscreen mode Exit fullscreen mode
php artisan breeze:install
Enter fullscreen mode Exit fullscreen mode
npm install && npm run dev
php artisan migrate
Enter fullscreen mode Exit fullscreen mode

Start the application

Start the development server

php artisan server
Enter fullscreen mode Exit fullscreen mode

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

Happy Coding!

Top comments (2)

saidbakr profile image
Said Bakr

I think it will be more handy if it able to, initially, create the .yaml file from the current database table schema.

dreadfulcode profile image
ƊŗęДdϝul Ȼʘɗɇ

You can generate laravel 10 eloquent models with relationships with this, will not need a yaml file