Blueprint

website·Indexed 35 days ago

https://blueprint.laravelshift.com/docs

An open-source code generation tool for Laravel developers that allows for the rapid generation of multiple components—including models, controllers, database seeders, and tests—from a single, human-readable domain language draft file.

Tokens
6.1K
Snippets
45
Records
61
Agent score
65%

What's inside Blueprint

  1. Overview of Blueprint for Laravel

    Blueprint is a code generation tool for Laravel developers that allows for the rapid development of multiple application components from a single, human-readable YAML domain language. It leverages conventions and shorthands to generate a wide array of Laravel components simultaneously, including:

    • Models
    • Controllers
    • Factories
    • Migrations
    • Form Requests
    • Events
    • Jobs
    • Mailables
    • Tests

    Code generation is executed via standard Laravel artisan commands.

  2. Overview of Laravel Blueprint

    Blueprint is an open-source tool designed to increase development speed and promote Laravel conventions by rapidly generating multiple Laravel components from a single, human-readable definition.
  3. Overview of Laravel Blueprint code generation

    Blueprint is a tool for Laravel developers that enables the rapid development of multiple application components from a single, human-readable YAML domain language. It leverages Laravel conventions and shorthands to generate the following components simultaneously: models, controllers, factories, migrations, form requests, events, jobs, mailables, and tests. The tool is integrated into the Laravel ecosystem via Artisan commands.
  4. Overview of Blueprint Controller Statements

    Blueprint uses a key: value syntax within controller actions to generate specific PHP code statements. These statements automate the creation of common Laravel patterns such as database queries, redirects, and response handling. Supported statement types include: delete, dispatch, find, fire, flash, notify, query, redirect, render, resource, respond, save, send, store, update, and validate.
  5. Understand Blueprint's generated HTTP tests

    Blueprint automatically generates an HTTP test for every controller action it creates. These tests follow the 'arrange, act, assert' pattern:

    • Arrange: Sets up model factories and mocks any Facades used in the controller action.
    • Act: Sends the appropriate HTTP request to the route, including necessary parameters and request data.
    • Assert: Verifies the HTTP response and the behavior of any mocks.

    These tests are intended as a foundation and should be reviewed and strengthened as the application evolves.

  6. Use implicit model columns in Blueprint

    Blueprint automatically generates the id column and timestamp columns (created_at and updated_at) for every model by default. You do not need to explicitly define these in your YAML draft unless you wish to change their types (e.g., using uuid for the ID).
  7. Infer and reference models in Blueprint controllers

    Blueprint automatically infers the related model based on the controller's name. For example, a PostController is assumed to relate to a Post model. Any statements within that controller that do not explicitly name a model (such as validate or save) will default to using the inferred model.

    To reference a different model, use the model's class name (e.g., User) or a namespaced class name relative to the model namespace (e.g., Admin\User). To reference a specific attribute on a model, use dot notation: Model.attribute (e.g., User.name).

    controllers:
      Post:
        index:
          query: all # Infers Post model
          render: post.index with:posts
        create:
          find: user.id # Explicitly references User model id
          render: post.create with:user
        store:
          validate: title, published_at # Infers Post model attributes
          save: post
          redirect: post.show
        show:
          query: all:comments # References comments relationship/model
          render: post.show with:post,comments
  8. Publish the Blueprint configuration file

    To customize Blueprint's default conventions, you must publish the blueprint.php configuration file to your application's config folder. This can be done either during the initialization of a new blueprint or as a standalone command.
    # Option 1: Publish while creating a new blueprint
    php artisan blueprint:new --config
    
    # Option 2: Publish as a standalone command
    php artisan vendor:publish --tag=blueprint-config
  9. Install Blueprint via Composer

    Blueprint should be installed as a development dependency in your Laravel application using Composer. Ensure you have a Laravel project initialized before running the installation command.
    composer require -W --dev laravel-shift/blueprint
  10. Define Eloquent model relationships in Blueprint

    Blueprint allows you to define Laravel Eloquent relationships within a relationships section of your model definition. Supported relationship types include belongsTo, hasOne, hasMany, and belongsToMany.

    For each defined relationship, Blueprint generates the corresponding Eloquent relationship method in the model class. For belongsToMany relationships, Blueprint automatically generates the required pivot table migration.

    Note: Referenced tables must exist. If migration order causes errors, you may need to add relationships manually after generation. To reference a model outside your application, use the fully qualified class name starting with a backslash (e.g., \Spatie\LaravelPermission\Models\Role).

    models:
      Post:
        title: string:400
        published_at: timestamp nullable
        relationships:
          hasMany: Comment
          belongsToMany: Media, Site
          belongsTo: \Spatie\LaravelPermission\Models\Role