Skip to main content

Claude/ChatGPT Prompt to Build a Laravel REST API to PSR Standards

Generate a Laravel 12 REST API with Form Requests, API Resources, Sanctum auth, policies, and PHPUnit feature tests to PSR-12.

Fill in the placeholders

Edit the values, then copy your finished prompt.

Your Prompt
prompt.txt

                                

What this prompt does

This prompt makes the model a senior Laravel engineer and asks for complete, PSR-12-compliant code with file paths rather than fragments. It defines six deliverables: a migration and Eloquent model with correct column types, casts, and a deliberately set fillable/guarded, FormRequest classes for store and update with real validation rules and authorize() logic, an API Resource and collection shaping responses to your chosen format, a resourceful controller with pagination and consistent JSON errors, a Policy enforcing per-record authorization wired to the auth model, and PHPUnit feature tests covering the happy path, validation failure, and an unauthorized-access case. The structure works because it puts Form Requests, Resources, and a Policy in from the start rather than bolting them on later.

Four variables drive it. [resource] names the API resource and its fields, like articles with title, body, author, and published_at. [laravel_version] sets the framework target, such as Laravel 12. [auth] chooses the authentication mechanism, like Sanctum token auth, which the Policy wires to. [response_shape] defines the envelope, such as data-wrapped JSON with meta pagination. The unauthorized-access test in deliverable six is the load-bearing part: a missing policy check is the leak that hurts, so generating that test guards against it. Setting fillable/guarded deliberately on the model matters too, since mass-assignment slips are easy and quiet.

When to use it

  • You are building a production Laravel REST API and want it structured correctly from the start.
  • You want Form Requests, API Resources, and a Policy generated together, not bolted on later.
  • Per-record authorization matters and you want a test that proves it.
  • A consistent response envelope with pagination meta is part of the contract.
  • You need PHPUnit feature tests covering happy path, validation failure, and unauthorized access.
  • You want PSR-12-compliant code delivered with explicit file paths.

Example output

Expect each file in its own fenced block headed by its path, targeting [laravel_version]. A migration and Eloquent model for [resource] set column types, casts, and fillable/guarded deliberately; store and update FormRequests carry real rules and authorize() logic; an API Resource and collection shape responses to [response_shape]; a resourceful controller adds pagination and consistent JSON errors; a Policy enforces per-record authorization wired to [auth]; and PHPUnit feature tests cover the happy path, a validation failure, and an unauthorized-access case. It is PSR-12-compliant, buildable code you drop into a Laravel project.

Pro tips

  • Spell out [resource] fields precisely, since they drive the migration columns, casts, and validation rules.
  • Match [laravel_version] to your project so generated syntax and conventions line up with your installed framework.
  • Set [auth] to your real mechanism so the Policy wires to the correct auth model.
  • Always keep the unauthorized-access test; a missing policy check is the leak that hurts most.
  • Set fillable/guarded intentionally on the model, because mass-assignment slips are easy and quiet.
  • If the [response_shape] envelope drifts, re-run specifying the exact data and meta structure you require.

Frequently Asked Questions

Does this generate authorization tests?
Yes. The PHPUnit feature tests cover an unauthorized-access case alongside the happy path and validation failure. A missing policy check is the kind of leak that hurts most, so generating that test explicitly guards against it.
Can I control the JSON response format?
The `[response_shape]` variable defines the envelope, defaulting to data-wrapped JSON with meta pagination. An API Resource and collection shape responses to that format, so set it to the contract your clients expect.
Which Laravel version and auth does it target?
The `[laravel_version]` variable defaults to Laravel 12 and `[auth]` to Sanctum token auth, with the Policy wired to your auth model. Set both to match your project so the generated code and conventions fit your installed framework.
Does it handle mass-assignment safety?
The prompt asks for fillable/guarded to be set deliberately on the model, since mass-assignment slips are easy and quiet. Combined with FormRequest validation, this keeps unexpected fields from being written through the API.
Engr Mejba Ahmed

Need this built for real?

Engr Mejba Ahmed

AI Developer · Software Engineer

I'm Mejba — I design and ship production AI systems, automations, and full-stack apps. If you want this turned into a working solution for your team, let's talk.

More in Laravel & PHP Prompts

Engr Mejba Ahmed

Engr Mejba Ahmed

Claude Code Expert · Online

👋

Hey there!

Quick Actions

WhatsApp Instant reply

Chat on WhatsApp

+880 1723 741224 · Instant reply

Popular Questions

Engr Mejba Ahmed is connected
Engr Mejba Ahmed is typing...
Engr Mejba Ahmed avatar

✉ Want me to follow up? Drop your email

Engr Mejba Ahmed avatar

📞 Connect Directly

Choose how you'd like to reach me

WhatsApp

+880 1723 741224

Email

[email protected]

✓ Details sent! I'll get back to you shortly.

Powered by OpenAI

335+

Blog Posts

25

AI Courses

63

Projects

Services & Expertise

Pricing & Process

Learning & Resources

Connect & Support