Sign in
Log inSign up

Laravel modeling using a low-code approach and cloud-native technology

Diving into the Laraboot Model buildpack

Oscar Nevarez's photo
Oscar Nevarez
·Oct 27, 2021·

5 min read

Laravel modeling using a low-code approach and cloud-native technology

In the first draft of what eventually would be Laraboot I envisioned the idea of these code blocks acting just like lego blocks, interchangeable pieces that read, create or mutate the state of an application. A lot has changed in the codebase to support this idea but the fundamental idea remains the same. I went from shell scripts to high-level language tools back and forth until I eventually adopt cloud-native technology, and particularly Paketo buildpacks as the common ground for the Laraboot platform.

The Laraboot model buildpack is a fully configurable buildpack that scaffold PHP code for the Laravel framework using community packages and the Laraboot platform. In a nutshell, this block will read the model definition from a laraboot specification file laraboot.json and execute the necessary commands for a parametrized code generation.

Providers

I never thought I'd need to support more than one package aka provider for a specific buildpack. But now I realize it makes the most sense in the world.

Blueprint

In the beginning, I fell in love with the simplicity of code generator Shift Blueprint so much that even created some GO helper tools to generate draft definitions.

Some of the features Shift Blueprint brings to the table are

  • Data model versioning using YML files?
  • CRUD routes and tests included?

Sign me in, right? Eventually, I noticed that although powerful this was a very much opinionated library and the coupling was increasing for the model buildpack codebase. If there were no changes it would have decided the course of the model buildpack as a whole. I decided to abstract my implementation and welcome other on board.

Larawiz

I don't know exactly how I missed it during my initial research, maybe I wasn't even looking. But when it came to me again I was completely fascinated.Larawiz is an easy project scaffolder for Laravel. It also uses the YAML file definition for the data model so the integration was as smooth as it could be.

Differences

Simplicity

  • Larawiz uses braindead easy syntax (quoting). No kidding! you can read the YAML file and completely understand what's going on there.
  • The same is true for Shift Blueprint but as I mentioned before, Shift Blueprint not only generates Models but routes and controllers so the file can get a little bunky.
#blueprint
models:
  Post:
    title: string:400
    content: longtext
    published_at: nullable timestamp
    author_id: id:user
#larawiz
models:
  Author:
    name: string
    email: string
    password: string
    publications: hasMany

  Publication:
    title: string
    body: longText
    author: belongsTo

Usage

Using the buildpack couldn't be easier. Read more about this buildpack.

1 - Install laraboot

npm i -g @laraboot-io/cli

2 - Create a laraboot application

laraboot new app --php-version=8.0.*
cd app

3 - Add buildpacks

laraboot task add @core/laravel-foundation-provider --format=file -vvv
laraboot task add @core/laravel-model-buildpack --format=file -vvv
laraboot build --pack-params default-process=task

4 - Define your data model

laraboot model add Record
√ Would you like to define your model now? (y/N) · true
√ Field name? · content
√ Field type? · varchar
√ Is this field unique? (y/N) · false
√ Is this field primary key? (y/N) · false
√ Would you like to add a new field? (y/N) · false
Model Record was added
laraboot model add Comment
√ Would you like to define your model now? (y/N) · true
√ Field name? · content
√ Field type? · varchar
√ Is this field unique? (y/N) · false
√ Is this field primary key? (y/N) · false
√ Would you like to add a new field? (y/N) · false
Model Comment was added
`

At this time the laraboot.json file was modified to include your data model. It will be something like this:

"models": [
      {
        "name": "Record",
        "columns": [
          {
            "name": "log",
            "type": "string"
          }
        ]
      },
      {
        "name": "Comment",
        "columns": [
          {
            "name": "comment",
            "type": "string"
          }
        ]
      }
    ]

You can modify the file directly if you forgot to add columns or want to make some modifications.

5 - Configure the buildpack

Now we need to tell the PHP buildpack how our app is configured. And also tell to the model buildpack what provider we want to use during the build process.

Below is a minimal configuration.

#buildpack.yml
php:
  # this allows you to specify a version constraint for the `php` dependency
  # any valid semver constraint (e.g. 7.* and 7.4.*) are also acceptable
  version: 8.0.x

  # text user can specify to use PHP's built-in Web Server
  # default: php-server
  webserver: php-server

  # directory where web app code is stored
  # default: htdocs
  webdirectory: public

  # directory where library code is stored
  # default: lib
  libdirectory: lib

  # Use default php script
  script: index.php

  # default: admin@localhost
  serveradmin: admin@localhost

  # default: redis-sessions
  redis:
    session_store_service_name: redis-sessions

  # default: memcached-sessions
  memcached:
    session_store_service_name: memcached-sessions

### add the following 
laravel-model:
  # larawiz or blueprint
  # provider: ~ 
  directory: app
  cleanup: true
  git:
    enabled: true
    commit: true

Build and run

# It takes a while the first time, be patient
laraboot build && laraboot run --port=8080

image.png

Congratulations! You have bootstrapped a Laravel application, and it's already containerized.

Conclusion

In this article, we saw how to generate PHP code for the Laravel framework using a low-code approach and cloud-native technology allowing us two choose from two different code-generator libraries. This opens the gates for future providers and makes the Laraboot platform the way to go to scaffold and maintain Laravel applications easily.

References

Hassle-free blogging platform that developers and teams love.
  • Docs by Hashnode
    New
  • Blogs
  • AI Markdown Editor
  • GraphQL APIs
  • Open source Starter-kit

© Hashnode 2024 — LinearBytes Inc.

Privacy PolicyTermsCode of Conduct