Today, I learned how to work with database schema, migration, and seeding in Leaf PHP (Leaf MVC). It helped me understand how Leaf manages database tables and dummy data in a clean and structured way.
1. Generating a Schema File
I learned how to generate a schema file using the Leaf CLI command:
leaf g:schema <table_name>
This command creates a schema file where I can define the table structure, columns, keys, and also add seeding logic.
2. Understanding the Schema File
The schema file contains:
- Table description
- Columns and data types
- Primary keys and relationships
- Seeder logic for inserting data
This makes it easy to manage both table creation and data seeding in one place.
3. Migrating Tables to the Database
I learned how to migrate tables using:
leaf db:migrate
Before running this command, the database must already exist. The migration command reads the schema files and creates the tables automatically.
4. Rollback, Reset, and Drop Commands
Leaf also provides helpful commands to manage database changes, such as:
- Rolling back migrations
- Resetting migrations
- Dropping tables
These commands make it easy to undo or modify database changes during development.
5. Seeding Dummy Data with Faker
Finally, I used Faker to seed dummy data into the database. This is useful for testing and development, as it quickly fills tables with realistic sample data.
Conclusion
Overall, today I learned how Leaf PHP simplifies database management using schema files, migrations, and seeders. These tools help keep the database organized, version-controlled, and easy to manage during development.
