Jeffrey Hicks

Jeffrey Hicks

Platform Eng @R360

15 - Phoenix crash course #15: CRUD generators (mix tasks)

Learn how to use Phoenix's mix generators to rapidly scaffold CRUD functionality for your applications

By Daniel Bergholz • Aug 19, 2025

Learn how to use Phoenix’s mix generators to rapidly scaffold CRUD functionality for your applications.

Mix tasks

Mix tasks in Phoenix are command-line generators/scaffolders, similar to Rails scaffolding.

CRUD generator types

  • mix phx.gen.context creates the schema, migration, and context for backend code only (no HTML/views/controllers)
  • mix phx.gen.html generates backend (schema, context, migration) and frontend CRUD (controllers, views, templates)
  • mix phx.gen.json creates backend and JSON API controllers/views
  • mix phx.gen.live makes LiveView scaffolds

Generator comparison

TaskSchemaMigrationContextControllerViewLiveView
phx.gen.embeddedx
phx.gen.schemaxx
phx.gen.contextxxx
phx.gen.livexxxx
phx.gen.jsonxxxxx
phx.gen.htmlxxxxx

Example

  • To generate a backend for a resource called “Console,” with fields name (string) and price (integer):
    mix phx.gen.context Consoles Console consoles name:string price:integer
  • To generate full HTML CRUD for “Promotion”:
    mix phx.gen.html Promotions Promotion promotions name code:unique expires_at:utc_datetime
    This also updates the router and supplies you with templates, a controller, forms, and even tests.

Workflow

  • After running a generator, run mix ecto.migrate to apply database changes
  • For HTML generators, add the suggested routes to your router.ex
  • Explore the UI and generated code to understand Phoenix conventions
  • Generator also provides test files and fixtures for easy TDD and unique field population

Tip

Frontend-focused devs may prefer just backend generators for control, but for rapid prototyping or learning Phoenix conventions, full HTML generators are very useful.

Resources

For complete documentation on Phoenix generators, see Mix.Tasks.Phx.Gen documentation.

Summary

Phoenix’s mix generators (phx.gen.context, phx.gen.html, etc.) help you rapidly scaffold idiomatic, fully-tested CRUD backends (and frontends) for new resources, streamlining both learning and production development.

Related

#phoenix-and-elixir #phoenix-crash-course #daniel-bergholz