Skip to content
Back to skills

Database And Models

ASecurity

Builds models, relationships, queries, migrations, schema changes, and factories in Laravel for Go. Use when creating or modifying models under models/, writing migrations under database/migrations/, querying the database, or defining factories.

  • 3 stars
  • 0 votes
  • 0 copies
  • 0 views
  • Added December 3, 2026
developmentrustgosqlapidatabase

Works with

  • cli
  • api

Security analysis

A100/100

Scanned December 3, 2026

npx -y skills add airshell/laravel-php-to-rust-go --skill database-and-models --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Database And Models?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Database And Models
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/airshell-database-and-models-laravel-php-to-rust-go/badge)](https://www.skillsdirectory.com/skills/airshell-database-and-models-laravel-php-to-rust-go)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: database-and-models
description: Builds models, relationships, queries, migrations, schema changes, and factories in Laravel for Go. Use when creating or modifying models under models/, writing migrations under database/migrations/, querying the database, or defining factories.
---

# Database, Models & Migrations

Consult the installed framework docs (locate via `.agents/skills/writing-laravel-go-apps/scripts/docs-path.sh`) for full API details: `models.md`, `model-relationships.md`, `migrations.md`, `schema.md`, `queries.md`, `pagination.md`, `factories.md`, and `database.md`.

## Models & Relationships

- Models are plain Go structs in `models/`—do not embed a base type. Add `json` tags on every field, use pointer fields (`*string`, `*time.Time`) for nullable columns, and add `db:",default"` for columns that have database defaults.
- Keep each model, its model-specific enums, and its authorization abilities together in `models/<model>.go` (unless the project already uses a separate `policies/` package), ordered top-down as model, then policy functions. Keep model factories in `database/factories/`.
- Pass a `context.Context` (`r` from handlers, `ctx` from jobs/actions, `client.Context()` from tests) to `model.Create`, `model.Save`, `model.Delete`, and to the methods that run a `model.Query[T]()` (such as `Get`). `model.Query[T]()` takes no context. Pass the request to `model.FromRoute[T]`—for example `model.FromRoute[models.Post](r, "post")` in a handler.
- Declare relationships with `model` struct tags (`model:"belongs_to"`, `model:"has_many"`, `model:"has_one"`, `model:"belongs_to_many"`) and `json:"...,omitzero"` so unloaded relationships are omitted from JSON.
- Relationships are never lazy-loaded in Laravel for Go. Eager load what you need with `.With("user", "comments")`, or query them explicitly with `model.Related`.
- Use `WithCount` instead of loading full relationships just to count them, chunk large datasets rather than loading everything into memory, and pair pagination with a deterministic `OrderBy` (adding a unique tie-breaker like `id` when order matters).
- Never interpolate untrusted input into raw SQL; bind parameters and allow-list any user-chosen sort columns.

```go
// models/post.go
package models

import "time"

type Post struct {
	ID          int64      `json:"id"`
	UserID      int64      `json:"user_id"`
	Title       string     `json:"title"`
	Body        *string    `json:"body"`
	Votes       int64      `json:"votes" db:",default"`
	PublishedAt *time.Time `json:"published_at"`
	CreatedAt   time.Time  `json:"created_at"`
	UpdatedAt   time.Time  `json:"updated_at"`

	User     *User     `json:"user,omitzero" model:"belongs_to"`
	Comments []Comment `json:"comments,omitzero" model:"has_many"`
}
```

## Migrations & Schema

- Generate migrations with `laravel make:migration create_posts_table` (or `go run . make:migration ...`), which writes the file in `database/migrations/` and registers it in `migrations.All` in `database/migrations/migrations.go`. If you write a migration by hand, add it to `migrations.All` yourself or it will never run.
- Migrations are forward-only—there is no `down` function or rollback command. Never edit or rename a migration that has already run in a shared or production environment; write a new forward migration instead. During local development before deploy, you can edit an unshipped migration and rebuild with `laravel migrate:fresh`.
- Laravel runs each migration inside a transaction carried by `ctx`—pass `ctx` to every `schema.*`, `db.*`, and `model.*` call. On MySQL and MariaDB, DDL statements commit implicitly, so keep each table change in its own migration file if targeting MySQL.
- Use the built-in helpers directly in `migrations.All` for framework-managed tables instead of hand-writing their schemas: `cache.CreateTables`, `feature.CreateTable`, `queue.CreateTables`, `session.CreateTable`, and `workflow.CreateTables`.

```go
// database/migrations/2026_11_10_120000_create_posts_table.go
package migrations

import (
	"context"

	"go.laravel.com/schema"
)

func createPostsTable(ctx context.Context) error {
	return schema.Create(ctx, "posts", func(table *schema.Blueprint) {
		table.ID()
		table.ForeignID("user_id").Constrained().CascadeOnDelete()
		table.String("title")
		table.Timestamp("published_at").Nullable()
		table.Timestamps()

		table.Index("user_id", "published_at")
	})
}
```

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in development →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.