# Codex Laravel: Practical AI Coding Agent Guide

URL: https://agentvera.dev/en/blog/codex-for-laravel/ · Published: 2026-11-01

Use Codex in Laravel with practical AGENTS.md setup, Eloquent migrations, queues, and PHPUnit or Pest workflows. Learn prompts, testing, and safe multi-agent runs.

**In short:**

- Codex becomes reliable in Laravel when AGENTS.md defines build, test, lint commands and Eloquent/migration rules.
- Work in small steps: plan, edit, run PHPUnit or Pest, and review git diffs before continuing.
- Use AgentVera for parallel agents in separate worktrees with code review, checkpoints, and token budgets.

Codex works well in Laravel projects when you treat it as a senior pair programmer that needs unambiguous project context. Start `codex` inside the Laravel folder, keep `AGENTS.md` precise, and drive work through small, testable steps. For CLI specifics, see [Codex](/en/agents/codex/) and [How to use Codex CLI: a step-by-step guide](/en/blog/how-to-use-codex-cli/). This guide covers setup, prompts, pitfalls, and verification for everyday Laravel development backed by Eloquent, migrations, queues, and PHPUnit or Pest.

## Eloquent, migrations, and queues: where Codex fits

Laravel has strong conventions: models map to tables, relationships are explicit, migrations should be append-only in team work, and Artisan commands generate predictable scaffolds. Codex can exploit those conventions if it knows your local rules. It reads Eloquent models and migrations, generates factories and seeders, suggests form requests for validation, and writes queued jobs with the retry semantics you specify. The main benefit is not magic generation but consistent changes that respect Laravel boundaries when you encode them in `AGENTS.md`.

## Preparing a Laravel project with AGENTS.md

Create an `AGENTS.md` file at the Laravel project root. Codex reads project instructions from that file automatically, so it becomes your source of truth for commands, conventions, and guardrails. Avoid vague advice; list the exact commands the agent should run and the Laravel-specific rules it must follow.

Here is a realistic starting point:

```markdown
# Laravel project rules

## Commands
- Install: composer install
- Build assets: npm run build
- Run tests: php artisan test --parallel
- Lint: vendor/bin/pint --test
- Static analysis: vendor/bin/phpstan analyse --memory-limit=1G

## Folder conventions
- Models live in app/Models and map to snake_case table names unless $table is set.
- Form requests live in app/Http/Requests; one per action.
- Jobs and mailables live in app/Jobs and app/Mail.
- Migrations are append-only; never edit an already merged migration.
- Keep controllers thin; move business logic to actions or services.

## Rules
- Use declare(strict_types=1); in PHP files.
- Prefer Eloquent relationships over raw joins.
- Queue all slow email or external API work.
- Blade templates get data only from controller variables; no queries in views.
- Use route model binding instead of manual find.
- Factories must reflect the current migration state.
```

If your project uses Laravel Sail, add `./vendor/bin/sail artisan test` as the test command. For multi-environment setups, tell the agent whether migrations should run with `--seed` and whether queue workers use `database`, `redis`, or `sync`. These details prevent the agent from assuming defaults that differ from your deployment. Update this file as your team adds commands such as `php artisan horizon`, `php artisan test --coverage`, or `php artisan ide-helper:models`.

## A practical loop: plan, edit, test, review

A reliable loop avoids large, unreviewable diffs. Ask Codex to plan first, then make one model/migration/controller/test change at a time. When you give Codex a task, include the affected route, model, and test file. Name the desired behavior, not just the code to write. After Codex edits, run the commands and inspect the diff. Use `git diff --stat` to keep changes reviewable. Then run the test suite and linter, review `git diff`, and commit only when the change is green.

Example prompts for common Laravel tasks:

- Add a `posts` table and a `Post` model with `title`, `slug`, `body`, and `published_at`. Include the migration, factory, seeder, and a Pest feature test for `GET /posts` listing published posts.
- Create a `SendWelcomeEmail` job that is queued after user registration. Use the database queue driver, write a failing test first, then implement the job and listener.
- Refactor `PostController@store` to use a `StorePostRequest` and a `CreatePost` action. Keep the existing feature tests green.
- Write an Artisan command `php artisan posts:prune-drafts` that deletes drafts older than 30 days. Include an isolated test for the command.
- Convert the `post-card` Blade partial to use route model binding and add a component test that checks the rendered link.

## Strong tasks and Laravel-specific pitfalls

Codex is strong at generating migration schema code, Eloquent models with casts and relationships, factories and seeders, form request validation, queued jobs and listeners, Artisan commands with signatures, and PHPUnit or Pest tests. It also handles refactors that require mechanical changes across many files, such as renaming a column or moving logic from a controller into an action.

Watch it closely in areas where Laravel’s magic hides consequences. It may add `DB::raw` or joins where an Eloquent relationship is clearer, forget `fillable` or `guarded` and introduce mass assignment risk, or introduce N+1 queries by accessing relations in a loop instead of eager loading. It can also write migrations that drop a column but fail to remove indexes or foreign keys first, or it may assume the `sync` queue driver when production uses `redis` or `database`. In Blade, it may use `{{ }}` where `{!! !!}` is intended or vice versa; ensure user input is escaped. With queues, a missing `ShouldQueue` interface turns an intended background job into a synchronous call. In tests, it may use `RefreshDatabase` but forget `DatabaseMigrations` or seed state, causing flaky tests. Always check `config/queue.php`, route model binding, and Blade escaping before accepting changes.

## Testing and verification workflows with Codex

Make the agent verify its own work. Ask it to run the exact commands from `AGENTS.md` after each change and to explain any failures before editing further. If CI fails, a good workflow turns the CI output into a prompt for Codex to reproduce and fix locally; for broader CI automation see [Auto fix CI failures and PR comments with AI](/en/blog/auto-fix-ci-failures-with-ai/).

The table below summarizes what to check per layer.

| Layer | Verification command | What to look for |
|---|---|---|
| Migrations | `php artisan migrate:fresh --seed` | Foreign key order, rollback safety, missing indexes |
| Models and relationships | `php artisan test --filter=Post` | Eager loading, casts, fillable, relationship names |
| Controllers and form requests | `php artisan test --filter=PostController` | Authorization, validation, route model binding |
| Jobs and queues | `php artisan test --filter=Jobs` | Queue driver assumptions, retry limits, unique jobs |
| Blade components | `npm run build` plus component tests | Escaped output, class names, missing variables |

Review the diff not only for syntax but for Laravel-specific consistency: `php artisan route:list` after route changes, `php artisan queue:restart` after code deploys that affects queued jobs, and `php artisan config:cache` only when configuration is correct. For Pest, the same workflow applies; replace PHPUnit test arguments with `php artisan test --filter` and keep the test naming consistent.

## Parallel agents on one repository with AgentVera

For Laravel projects with several work streams, AgentVera runs multiple CLI agents in real terminal panels, each in its own git worktree to avoid overwriting files. That lets one agent build a queued job while another writes migrations. Automatic code review after each turn catches missing `fillable` and N+1 issues with file:line links, which is especially valuable before merging Eloquent changes. Checkpoints let you undo a bad refactor, and token budgets stop an agent from churning through a long test suite. See [AI code review: check what your agents write](/en/blog/ai-code-review/).

## Closing notes

Codex on Laravel becomes predictable when you treat it as a tool driven by explicit project rules and small verification loops. Put the real commands and Laravel conventions in `AGENTS.md`, insist on tests after each change, and review diffs for Eloquent and migration pitfalls. That approach keeps the agent useful without allowing framework-level surprises into your codebase.

## Frequently asked questions

**How do I set up Codex for a Laravel project?** Create an AGENTS.md at the project root with the exact install, test, lint, and build commands, plus Laravel-specific rules such as append-only migrations and Eloquent conventions. Start codex in the same folder so it reads the file automatically. Then ask for small changes and run the test suite after each step.

**Can Codex write Laravel migrations and Eloquent models?** Yes, Codex handles schema builder migrations, model relationships, casts, factories, and seeders well when given clear instructions. Review generated migrations for foreign key ordering and rollback safety, and check models for fillable, guarded, and eager loading.

**How do I get Codex to run Laravel tests and fix failures?** Ask it to run php artisan test or vendor/bin/pint --test after each change and to explain any failures before editing. Feed CI output or failing test names back into the prompt so it reproduces the issue locally.

**Can I run multiple Codex agents on the same Laravel repository?** A single repository works best with one agent at a time unless you use a tool like AgentVera to isolate work. AgentVera runs each agent in its own git worktree so parallel changes do not overwrite each other, with code review and checkpoints for safety.

---

**About AgentVera.** AgentVera is a desktop app for macOS, Windows and Linux, built by Solviera Teknoloji. It runs 18 AI coding agents (Claude Code, Codex, Gemini CLI, Copilot, Cursor, Antigravity, opencode, Hermes, Qwen Code, Kimi Code, Kiro, Amp, Droid, Goose, Vibe, Crush, Aider, Cline) side by side, per project, in real terminals with the user's own CLI installs and logins. The free plan has unlimited agents. Download: https://agentvera.dev/en/download/ · Pricing: https://agentvera.dev/en/pricing/
