Reducing Laravel Collections to a Single Value
After PHP code loads records into a Laravel collection, reduce() processes each record and carries a running value from one record to…
When you add a feature that follows common Laravel patterns, you often need several files to work together, including a model and controller, validation and tests, plus a database migration that changes the database structure. Laravel Shift’s Blueprint uses a short YAML draft, a readable text file describing models and controller actions, to generate an initial version of those files. You can review and edit the generated Laravel code just as you would code you wrote by hand.
Blueprint is most useful when you repeat familiar Laravel patterns across several files. It does not decide what a feature should do or check whether the generated code meets your requirements. Laravel also has a separate Illuminate\Database\Schema\Blueprint class, which developers use in migration files to define database tables.
Blueprint reads a draft file and uses its definitions and Laravel conventions to generate project files. Run Blueprint with Artisan, Laravel’s command-line tool. By default, the build command reads draft.yaml or draft.yml in the project root.
A draft can describe models, their fields and relationships, controllers, and controller actions. Blueprint uses those descriptions to generate related code, such as migrations and other components. It also applies Laravel conventions to fill in some details. For example, Blueprint infers common HTTP methods from action names. Check those inferences in the generated code. The component-generation documentation explains how Blueprint applies these conventions.
The project’s README shows an example where 20 lines of YAML generate 12 types of output, including a model, migration, factory, controller, routes, form request, and tests. This example shows what one draft can produce; it does not mean every draft generates all 12 types. The README example shows the different files Blueprint can generate together.
Here is a small draft that defines a post model and two controller actions:
models:
Post:
title: string:400
content: longtext
published_at: nullable timestamp
controllers:
Post:
index:
query: all
render: post.index with:posts
store:
validate: title, content
save: post
redirect: posts.index
The models section defines fields and their data types. The controllers section describes what each action does. Here, index retrieves posts and displays them in a view; store checks the title and content, saves a post, and redirects to the posts index. Blueprint uses statements such as query, render, validate, and save to generate controller code and related files. The model documentation explains how field definitions shape the generated files.
Install Blueprint as a development dependency, then create a Blueprint draft file:
composer require -W --dev laravel-shift/blueprint
php artisan blueprint:new
After writing the draft, run php artisan blueprint:build. Blueprint generates the files, but you still need to inspect the migration and run it when it fits your project. Treat the generated code as a starting point. Check its validation, database changes, and behavior against the feature requirements before using it.
Before generating code, make sure your Git working tree has no uncommitted changes, or commit your current changes. Then build from the draft and inspect the diff, which shows what files Blueprint added or changed. If the output does not fit the project, use version control to revert specific changes.
Check the database migrations and relationships carefully. Blueprint supports common Eloquent relationships, including belongsTo, hasOne, hasMany, and belongsToMany. For a many-to-many relationship, Blueprint can generate a pivot-table migration, a migration that creates the table linking the related records. The migrations can still fail if one runs before a table it references exists. Check that they run in the intended order. If they do not, adjust the relationship or migration yourself. The relationship documentation describes these limits.
Review the generated tests, too. Blueprint creates starter tests that you need to build on; they are not a complete test strategy. These tests assume the project has some default elements, including a User model and app.blade.php. If your project lacks either one, change the tests before running them. Read the generated-test guidance and add checks for the behavior your feature requires.
Blueprint’s blueprint:erase command removes files from its last build, but it cannot fully undo every change. Preserve a clean baseline before building so you can use version control to choose exactly what you revert. If your draft refers to models already in the project, run blueprint:trace to update Blueprint’s local model cache. Run it again after those models change, because the cached information can become outdated. The command documentation covers both commands.
Use Blueprint for features that follow the project’s Laravel conventions. A developer can describe a model and its common actions once, then review the related files together instead of creating each file separately. Developers can use custom stubs and extensions to match the generated code to project conventions, but they must also maintain those customizations. The extension documentation explains how to add generation behavior.
A draft may not describe enough of a feature with unusual architecture or business logic. Use Blueprint for the standard scaffolding it supports, then write or revise the specialized code yourself. The project documentation does not measure time savings or defect rates, and it does not guarantee that generated code is ready for production. Expect Blueprint to reduce repetitive setup, then review and test the code to judge the result.
According to the project’s README and release information, Blueprint v2 requires Laravel 11 or later. The Getting Started page still says Laravel 6 or later. Check the compatibility details for the package version you plan to install instead of relying on that older statement.
Give Vroni a GitHub issue, bug report, spec, or rough idea. It reads the repo, plans the change, writes code, runs checks, and works toward a review-ready pull request.
Take a look at vroni.com