Skip to content

Instantly share code, notes, and snippets.

@dwoodard
Last active November 13, 2024 03:43
Show Gist options
  • Save dwoodard/8c28d0fabb52f49ef5555f02d9281c2e to your computer and use it in GitHub Desktop.
Save dwoodard/8c28d0fabb52f49ef5555f02d9281c2e to your computer and use it in GitHub Desktop.
Understanding the Core Patterns of Blueprint
Blueprint allows you to define your Laravel application's components—like models and controllers—in a concise, human-readable YAML syntax. The key is to recognize the consistent patterns in how these definitions are structured.
Let's focus on simplifying models and controllers, as they are foundational to any Laravel application.
## Models
At its simplest, a model in Blueprint is defined by:
- **Model Name**: The singular, capitalized name of the model.
- **Fields**: Key-value pairs where the key is the field name, and the value is the data type (with optional modifiers).
- **Relationships (optional)**: Define associations with other models.
### Basic Structure
```yaml
models:
ModelName:
field_name: data_type[:modifiers]
# Blueprint Syntax Overview
models:
[ModelName]:
# Fields/Columns
[field_name]: [data_type][:modifiers]
# Examples:
# name: string:unique
# email: string:unique:nullable
# age: integer:default(18)
# is_active: boolean:default(true)
# bio: text
# created_at: timestamp
# price: decimal(8,2)
# image: nullable uuid
# Relationships
relationships:
[relationship_type]: [RelatedModel]
# [relationship_type] can be one of:
# hasOne
# hasMany
# belongsTo
# belongsToMany
# morphOne
# morphMany
# morphTo
# morphToMany
# Examples:
# hasOne: Profile
# hasMany: Comment
# belongsTo: User
# belongsToMany: Role
# morphOne: Image
# morphMany: Tag
# Indexes
indexes:
- [field_name][:index_type]
# [index_type] can be:
# unique
# index
# fulltext
# Examples:
# - email: unique
# - title: fulltext
# - created_at
# Timestamps and Soft Deletes
timestamps: [true|false] # default is true
softDeletes: [true|false] # default is false
# Additional Model Options
primaryKey: [field_name]
incrementing: [true|false]
keyType: [string|int]
rememberToken: [true|false]
table: [table_name]
connection: [connection_name]
attributes:
[attribute_name]: [value]
casts:
[field_name]: [cast_type]
appends:
- [attribute_name]
hidden:
- [field_name]
visible:
- [field_name]
fillable:
- [field_name]
guarded:
- [field_name]
touch: [relation_name]
with:
- [relation_name]
withCount:
- [relation_name]
observers:
- [ObserverClass]
events:
[event_name]: [ListenerClass]
# [event_name] can be:
# retrieved
# creating
# created
# updating
# updated
# saving
# saved
# deleting
# deleted
# restoring
# restored
scopes:
[scope_name]:
[condition]
# Define query scopes
# Example:
# active:
# where: is_active, true
controllers:
[ControllerName]:
[method_name]:
# Statements/Actions
[statement]: [parameters]
# Common statements:
# render: [view_name] with:[data]
# redirect: [route_name]
# validate: [field1], [field2], ...
# save: [model_instance]
# update: [model_instance]
# delete: [model_instance]
# query: [query_type]
# where: [field], [value]
# orderBy: [field], [direction]
# limit: [number]
# find: [id|$id]
# send: [MailableClass] to:[recipient] with:[data]
# dispatch: [JobClass] with:[data]
# fire: [EventClass] with:[data]
# authorize: [ability]
# policy: [action] [model_instance]
# resource: [model_instance]
# paginate: [per_page]
# call: [service.method] with:[data]
# dump: [variable]
# dd: [variable]
# flash: [message]
# session: [key], [value]
# download: [file_path]
# stream: [file_path]
# abort: [status_code]
# respond: [status_code] with:[data]
# json: [data]
# back: with:[input]
# view: [view_name] with:[data]
# middleware: [middleware_name]
# before: [filter_name]
# after: [filter_name]
# uses: [Controller@method]
# inject: [variable] into:[parameter]
# model: [model_instance] from:[parameter]
# eagerLoad: [relation]
# sync: [relation] with:[ids]
# attach: [relation] with:[id]
# detach: [relation] with:[id]
# log: [level], [message]
# cache: [key] for:[duration] do:[statements]
# share: [variable] with:[view]
# emit: [event_name] with:[data]
# sessionFlash: [key], [value]
# header: [key], [value]
# cookie: [key], [value]
# input: [key]
# request: [key]
# Additional Controller Options
resource: [true|false] # Generates resource controller methods
apiResource: [true|false] # Generates API resource controller methods
invokable: [true|false] # For single-action controllers
middleware:
- [middleware_name]
seeders:
- [SeederName]
# Examples:
# - UserSeeder
# - PostsTableSeeder
tests:
[TestName]:
[method_name]:
# Test statements/actions
[action]: [parameters]
# Common test statements:
# call: [method], [route_name] with:[data]
# assertViewIs: [view_name]
# assertViewHas: [data]
# assertRedirect: [route_name]
# assertDatabaseHas: [table], [data]
# assertDatabaseMissing: [table], [data]
# assertSee: [text]
# assertDontSee: [text]
# assertJson: [data]
# assertStatus: [status_code]
# assertSessionHas: [key], [value]
# assertCookie: [key], [value]
# actingAs: [user]
# mock: [Class] with:[methods]
# spy: [Class] with:[methods]
# expectsEvents: [EventClass]
# withoutEvents
# withoutMiddleware
# assertAuthenticated
# assertGuest
# seed: [SeederClass]
factories:
[ModelName]:
[field_name]: [faker_method]
# Examples:
# name: $faker->name
# email: $faker->unique()->safeEmail
# created_at: $faker->dateTimeBetween('-1 year', 'now')
emails:
[MailableClass]:
from: [email_address]
to: [email_address]
cc: [email_address]
bcc: [email_address]
subject: [subject_line]
markdown: [view_name]
view: [view_name] with:[data]
attachments:
- [file_path]
content:
[key]: [value]
jobs:
[JobClass]:
# Job properties and methods
handle:
[action]: [parameters]
# Examples:
# queue: [queue_name]
# connection: [connection_name]
# delay: [seconds]
events:
[EventClass]:
# Event properties
properties:
[property_name]: [type]
# Examples:
# userId: int
# payload: array
commands:
[CommandName]:
signature: [signature]
description: [description]
handle:
# Command actions
[action]: [parameters]
notifications:
[NotificationClass]:
via: [channels]
toMail:
subject: [subject_line]
markdown: [view_name]
view: [view_name] with:[data]
toDatabase:
[key]: [value]
toArray:
[key]: [value]
channels:
[ChannelName]:
method: [method_name]
# Examples:
# UserChannel:
# method: join
policies:
[PolicyClass]:
[ability]:
[condition]
# Examples:
# view: $user->id === $model->user_id
# update: $user->isAdmin()
rules:
[RuleClass]:
passes: [condition]
message: [error_message]
middleware:
[MiddlewareClass]:
handle:
[action]: [parameters]
providers:
[ServiceProviderClass]:
register:
[action]: [parameters]
boot:
[action]: [parameters]
routes:
[route_name]:
uri: [uri]
method: [HTTP_method]
action: [Controller@method]
middleware:
- [middleware_name]
name: [route_name]
channels:
[channel_name]:
type: [type]
route: [model]
pattern: [pattern]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment