Continuous Laravel Blueprint DX

Laravel Blueprint is a wonderful tool that helps scaffold Laravel apps in the early stage of development.

It uses a simple YAML format, like the below:

Then run php artisan blueprint:trace and the models, migrations, factories, Nova resources will all be generated.

This saves a lot of time, but once it has been generated once, they can’t be generated again for fear of overwriting changes that have been made in developing the application.

A technique I’ve been using is to separate the generated model code and the model behaviour into a separate trait.

To achieve this, I’ve created a custom ModelGenerator which extends the original ModelGenerator, and if a behaviour for the model exists, it will inject the trait into the generated model class.

Then in config/blueprint.php, opt in for the new model generator:

'model' => \App\Support\ModelGenerator::class,

Here’s the resulting classes:

With this approach I can freely tinker with the draft.yaml file and generate without losing any of my local changes. This could be extended to the other objects that Blueprint can generate for you — controllers, form requests etc..

Let me know if this works for you!