laravel-model-migration

Guides Eloquent model and migration changes in the Toollab Laravel API with trait, fillable, and foreign key conventions.

Updated May 5, 2025
One-click install
npx skills add https://github.com/sebauvray/toollab-api --skill laravel-model-migration-sebauvray
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: laravel-model-migration
Source: https://github.com/sebauvray/toollab-api/tree/main/.claude/skills/laravel-model-migration
Command: npx skills add https://github.com/sebauvray/toollab-api --skill laravel-model-migration-sebauvray

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Evolving the Toollab API database schema is risky: choosing the wrong Eloquent trait breaks multi-tenant scoping, a missing onDelete policy silently blocks deletions in MariaDB, and a NOT NULL column added without backfill corrupts existing data. This Skill encodes the project's exact conventions so every model or migration change is safe and consistent. ## Core Features & Use Cases - Trait decision tree: Determines when to apply BelongsToSchool, BelongsToSchoolYear, and TrackChangesTrait, with the matching columns each trait requires. - Migration conventions: Enforces anonymous-class migrations, explicit cascadeOnDelete/restrictOnDelete/nullOnDelete policies, defensive down() methods, and named composite unique indexes. - Safe NOT NULL rollout: Provides the three-step pattern (nullable column, raw SQL backfill, guard check before locking) plus known pitfalls like the polymorphic user_roles table and N+1 $appends accessors. - Use Case: You need to add a school_year_id column to an existing table with production data. The Skill walks you through the nullable-add, query-builder backfill, orphan guard, and verification commands before you deploy. ## Quick Start Ask the assistant to add a new Eloquent model or modify a migration column in the Toollab API following the project conventions.

Frequently Asked Questions about laravel-model-migration

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I add a NOT NULL column to an existing Laravel table with data?▼

Add the column as nullable first, backfill existing rows with a raw DB::statement or query builder update, then verify zero orphans remain before altering the column to NOT NULL. Throw an exception in the migration if the backfill is incomplete.

How do I choose between cascadeOnDelete, restrictOnDelete, and nullOnDelete in Laravel migrations?▼

Use cascadeOnDelete for strong dependencies where a child is meaningless without its parent, restrictOnDelete to protect structural references like school_year_id, and nullOnDelete for optional links. Always write the policy explicitly, since a bare constrained() defaults to RESTRICT in MariaDB.

Why does eager loading a Laravel relationship return empty results?▼

If the relationship closure references $this->id inside a whereHas, it is null during eager loading, so with() always returns empty. Batch the query manually with whereIn on the parent IDs and group the results instead.

Can I use Eloquent models inside a Laravel migration for backfills?▼

No, migrations run without HTTP context, so auth-dependent global scopes and helpers like currentSchoolId() return null or fail closed. Use DB::table() query builder statements instead of Eloquent in migrations.

What are the risks of polymorphic tables without foreign keys in Laravel?▼

A polymorphic column like roleable_id cannot have a foreign key, so no cascade cleans up orphaned rows when the parent is deleted. Every deletion of a roleable entity must explicitly delete its matching polymorphic rows.