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 uses a simple YAML syntax to define your models and controllers offering shorthands and leveraging conventions to maximize the developer experience.
Blueprint comes with artisan commands making it easy and familiar to build new components and reference existing within your Laravel application.
Blueprint not only generates models and controllers, but factories, migrations, form requests, events, jobs, and mailables. Oh, and tests too.
laravel new blue-app cd blue-app
composer require --dev laravel-shift/blueprint
Testing package for tests created
composer require --dev jasonmccreary/laravel-test-assertions
Ignoring Blueprint files
echo '/draft.yaml' >> .gitignore echo '/.blueprint' >> .gitignore
Create a draft.yaml file
Add the following
models: Post: title: string:400 content: longtext remark: string:100 nullable user_id: id foreign published_at: nullable timestamp relationships: hasMany: Transaction belongsTo: User Transaction: payment_token: string:40 total: decimal:8,2 user_id: id foreign post_id: id foreign:posts status: enum:pending,successful,failed relationships: belongsTo: User, Post seeders: Post, Comment controllers: Post: index: query: all:posts render: post.index with:posts create: render: post.create store: validate: title, content, remark save: post send: ReviewNotification to:post.author with:post dispatch: SyncMedia with:post fire: NewPost with:post flash: post.title redirect: post.index show: render: post.show with:post edit: render: post.edit with:post update: validate: post update: post flash: post.id redirect: post.index destroy: delete: post redirect: post.index Transaction: store: validate: transaction save: transaction flash: transaction.id redirect: post.index show: render: transaction.show with:transaction Report: invokable: fire: ReportGenerated render: report
modelclass for Post, Transaction complete with fillable, casts, and dates properties, as well as relationships methods.
migrationto create the posts and transaction tables.
factoryintelligently set with fake data.
controllerclass for PostController with index and store actions complete with code generated for each statement.
web.phpfor the all controllers' actions.
form requestof 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.
Using the Breeze for Authentication code scaffolding.
composer require laravel/breeze --dev
php artisan breeze:install
npm install && npm run dev php artisan migrate
Start the development server
php artisan server
Access from the browser at http:://localhost:8000