In this episode, we dive into setting up the core data structures for our app: employees and services, plus how they relate to each other. We start by creating an Employee model along with a migration and factory, focusing on the basic fields an employee needs (name, slug for URLs, and a profile photo URL). After building the employees table, we add a couple of sample employees to test things out.
Next, we move on to the Service model, again making sure it has a migration and factory. Our services have a title, slug, duration (in minutes), and price (stored as an integer, like cents, for easier formatting later). We add a couple of example services to the database, just like we did with employees.
The fun part is linking employees to the services they can perform. We do this by creating a pivot table (employee_service
). This lets us specify, for example, that Alex can cut hair and trim beards, but Mabel can only cut hair. We then set up the many-to-many relationships in the Employee and Service models, making it super easy to find out which employee can do which services (and vice versa).
After testing that this linking works by dumping out some results in the browser, we quickly flesh out our factories. This ensures we can easily generate fake employees and services (with slugs and profile photo URLs) for future tests or seeding.
By the end of the episode, we have a solid setup where employees and services are connected, and it will be easy to expand on this as we add more features to the app!