Understanding Migrations
Migrations are version control for your database schema. Each migration file represents a change to the database structure.Migration File Structure
Migrations are located indatabase/migrations/ and follow this naming pattern:
Available Migrations
The ServITech Backend includes these migrations (in execution order):0001_01_01_000000_create_users_table.php
0001_01_01_000000_create_users_table.php
Creates the core user authentication tables:Tables Created:
users- User accounts with name, email, phone, passwordpassword_reset_tokens- Password reset tokenssessions- User session storage
id- Primary keyname- User’s full nameemail- Unique email addressphone- Optional phone numberpassword- Hashed passwordemail_verified_at- Email verification timestampdeleted_at- Soft delete support
0001_01_01_000001_create_cache_table.php
0001_01_01_000001_create_cache_table.php
Creates tables for Laravel’s database cache driver:Tables Created:
cache- Cached data storagecache_locks- Distributed lock mechanism
0001_01_01_000002_create_jobs_table.php
0001_01_01_000002_create_jobs_table.php
Creates queue and job tracking tables:Tables Created:
jobs- Pending queue jobsjob_batches- Batch job trackingfailed_jobs- Failed job records
2025_03_21_072704_create_permission_tables.php
2025_03_21_072704_create_permission_tables.php
Creates role and permission system tables (Spatie Laravel Permission):Tables Created:
permissions- Available permissionsroles- User rolesmodel_has_permissions- Direct permission assignmentsmodel_has_roles- User-role assignmentsrole_has_permissions- Role-permission assignments
2025_03_21_243245_create_categories_table.php
2025_03_21_243245_create_categories_table.php
Creates the product categories table:Fields:
id- Primary keyname- Category namedeleted_at- Soft delete supporttimestamps- created_at, updated_at
2025_03_23_223703_create_subcategories_table.php
2025_03_23_223703_create_subcategories_table.php
Creates the product subcategories table:Fields:
id- Primary keycategory_id- Foreign key to categories (cascade delete)name- Subcategory namedeleted_at- Soft delete supporttimestamps- created_at, updated_at
2025_03_23_235144_create_images_table.php
2025_03_23_235144_create_images_table.php
Creates the images table for storing product images:Fields:
id- Primary keyimageable_id- Polymorphic relation IDimageable_type- Polymorphic relation typeurl- Image URL or pathdeleted_at- Soft delete supporttimestamps- created_at, updated_at
2025_03_23_235722_create_repair_requests_table.php
2025_03_23_235722_create_repair_requests_table.php
Creates repair request tracking for technical support:Fields:
id- Primary keyuser_id- Customer who requested repairreceipt_number- Unique receipt identifierdevice_type- Type of devicebrand- Device brandmodel- Device modelissue_description- Problem descriptionstatus- Current repair statusdeleted_at- Soft delete supporttimestamps- created_at, updated_at
2025_03_25_005226_create_articles_table.php
2025_03_25_005226_create_articles_table.php
Creates the articles (products) table:Fields:
id- Primary keycategory_id- Foreign key to categories (cascade delete)subcategory_id- Foreign key to subcategories (cascade delete)name- Product namedescription- Product descriptionprice- Product price (float)deleted_at- Soft delete supporttimestamps- created_at, updated_at
2025_03_26_165534_create_support_requests_table.php
2025_03_26_165534_create_support_requests_table.php
Creates technical support request tracking:Fields:
id- Primary keyuser_id- Customer requesting supportsubject- Support request subjectdescription- Detailed problem descriptionstatus- Request status (open, in_progress, closed)priority- Priority leveldeleted_at- Soft delete supporttimestamps- created_at, updated_at
Running Migrations
First Time Setup
Run all pending migrations:Migrations are tracked in the
migrations table. Laravel only runs migrations that haven’t been executed yet.Check Migration Status
View which migrations have been run:- ✓ Ran migrations (green)
- ✗ Pending migrations (yellow)
Rolling Back Migrations
Undo the last batch of migrations:Fresh Migration
Drop all tables and re-run all migrations:Refresh Migration
Rollback all migrations and run them again:Creating New Migrations
Basic Migration
Create a new migration file:Migration with Model
Create a model with migration:app/Models/ModelName.phpdatabase/migrations/YYYY_MM_DD_HHMMSS_create_model_names_table.php
Example Migration Structure
database/migrations/YYYY_MM_DD_create_example_table.php
Database Seeders
Seeders populate your database with test or initial data.Available Seeders
Located indatabase/seeders/:
- DatabaseSeeder.php - Main seeder that calls all others
- UserSeeder.php - Creates test users with roles
- CategorySeeder.php - Adds product categories
- SubcategorySeeder.php - Adds subcategories for each category
- ArticleSeeder.php - Generates sample products
- PermissionSeeder.php - Creates permissions
- RoleSeeder.php - Creates user roles
Running Seeders
Run all seeders:Seeder Execution Order
TheDatabaseSeeder runs seeders in this order:
database/seeders/DatabaseSeeder.php
Order matters! Seeders with foreign key dependencies must run after their parent tables are seeded.
Creating a New Seeder
Generate a seeder class:database/seeders/ProductSeeder.php
DatabaseSeeder.php:
Common Workflows
Initial Setup
Reset and Reseed
When you need fresh data:- Drops all tables
- Runs all migrations
- Runs all seeders
Adding a New Feature
Troubleshooting
Foreign Key Constraint Fails
Error:SQLSTATE[23000]: Integrity constraint violation
Cause: Trying to insert data with foreign keys that don’t exist.
Solution: Ensure parent records exist first or run seeders in the correct order.
Migration Already Exists
Error:Migration already exists
Solution:
- Check
database/migrations/for duplicate files - Or rollback and re-run:
Table Already Exists
Error:SQLSTATE[42S01]: Base table or view already exists
Solution:
Seeder Class Not Found
Error:Target class [SeederName] does not exist
Solution: Ensure proper namespace and autoload:
SQLite Database Locked
Error:database is locked
Cause: Another process is using the SQLite database.
Solution:
- Stop all running PHP processes
- Restart the development server
- Or switch to MySQL for better concurrency
Database Maintenance Commands
View Database Schema
Interactive Database Shell
Wipe Database
Drop all tables without running migrations:Best Practices
Never Edit Existing Migrations
Once a migration has been committed and shared, create a new migration to modify the schema.
Always Use Soft Deletes
Include
$table->softDeletes() for data recovery. Already implemented in all ServITech tables.Use Descriptive Names
Migration names should clearly describe the change:
add_status_to_orders_tableTest Rollbacks
Ensure your
down() method properly reverses the up() method changes.Next Steps
Testing
Learn how to write and run tests for your API
API Reference
Explore the complete API endpoint documentation