The benefits of continiously rebuilding your database in Laravel
I just came across this great post by Stefan Zweifel about using seeders and factories for your local development environment: "Local Environment Seeders in Laravel". The post discusses topics that are closely aligned with my preferred way of working.
One of the first things I do in a fresh project is adding a custom command
php artisan db:refresh that in essence is just an alias of
php artisan migrate:fresh --seed. This command will clear your database, run all migrations and run all seeds.
I run this command frequently. Switching branches? db:refresh. Starting my workday? db:refresh, etc.
This approach forces you to have good seeds all the time. This way seeds won't be an afterthought but an essential part of your process. Which brings a lot of benefits:
- You are quickly up and running when switching machines.
- Reduces the complexity of "setting up" a project for people new to the team.
- Need to demo the project? No worries, just refresh the database and you have clean data to give a polished demo.
- Don't worry about state: there's much less need to ask your coworkers how to get a certain user in a specific state. This can be set up in the seeds and documented.
- Good factories are great for testing.
All these things take away some cognitive load which you can use to focus on developing the application.
You can read more about this in Stefans blogpost: https://stefanzweifel.dev/posts/2023/01/30/local-environment-seeders-in-laravel