Complete Laravel feature: migration, policy, controller and tests
Build a Laravel feature end to end: migration, model, Form Request, Policy, controller, routes and Pest or PHPUnit feature tests, with no N+1 queries. Designed for ChatGPT, Claude, Gemini and Microsoft Copilot. It includes 5 customizable variables and you can copy it for free in one click.
Intermediate · Code · Pending editorial review
Prompt
Act as a senior PHP developer specializing in Laravel and maintainable code.
I need to implement this feature in a Laravel {{laravel_version}} application: {{feature}}.
- Entities and relationships: {{entities}}
- Roles and permissions: {{roles}}
- Test framework: {{tests}}
Deliverables, in this order:
1. Design summary: tables, Eloquent relationships, routes and which layer does what (controller, Form Request, Policy, action or service class).
2. Migrations with foreign keys, indexes and explicit `onDelete`, plus models with `$fillable` or `$guarded`, `casts` and relationships.
3. Form Requests with validation rules and messages.
4. A registered Policy, enforced with `authorize` or the `can` middleware, covering every role.
5. A thin controller (resource or single-action) that delegates business logic to an action class, using `DB::transaction` where several writes happen.
6. Named routes with middleware and route model binding.
7. Output: an API Resource for an API, or minimal Blade views for a web app. Eager loading (`with`) to avoid N+1 queries.
8. Factories and feature tests: happy path, failed validation, unauthorized user (403) and someone else's record.
9. Artisan commands to generate the files and run the tests.
Constraints:
- Follow Laravel conventions and PSR-12; no logic in route files.
- Do not invent columns, roles or rules: ask me if something is missing or use [FILL IN].
- If the chosen version changes anything (for example the `bootstrap/app.php` structure), say so.
Format: one code block per file with its path, then a final list of integration steps.How to use it
- Describe the feature as a user story with its rules.
- List the tables that already exist so nothing gets duplicated.
- Generate the files with Artisan, paste the code and run the tests.
- Check the Policy against every role before deploying.
Usage example
Feature: customers request quotes and sales reps approve them · Entities: Customer, Quote, QuoteLine · Laravel 11 · Pest.
Sample output
4. Policy — app/Policies/QuotePolicy.php
<?php
namespace App\Policies;
use App\Enums\QuoteStatus;
use App\Models\Quote;
use App\Models\User;
class QuotePolicy
{
// Admins can do everything; returning null lets the other methods decide
public function before(User $user): ?bool
{
return $user->isAdmin() ? true : null;
}
public function view(User $user, Quote $quote): bool
{
return $user->isSalesRep() || $quote->customer->user_id === $user->id;
}
public function create(User $user): bool
{
return $user->isCustomer();
}
public function approve(User $user, Quote $quote): bool
{
return $user->isSalesRep() && $quote->status === QuoteStatus::Pending;
}
}
In Laravel 11 the policy is auto-discovered when it follows the App\Models\Quote → App\Policies\QuotePolicy convention, so there is no AuthServiceProvider to register it in.
Integration steps
php artisan make:policy QuotePolicy --model=Quote- In the controller:
Gate::authorize('approve', $quote); - Pest test: a customer trying to approve gets 403, and so does a customer opening someone else's quote.
isAdmin(),isSalesRep()andisCustomer()depend on how you store roles → [FILL IN]…
Key facts
- Category
- Programming & development · PHP
- Output type
- Code
- Level
- Intermediate
- Designed for
- ChatGPT, Claude, Gemini and Microsoft Copilot
- Verified in
- Pending editorial verification
- Variables
- Feature, Entities and relationships, Roles and permissions, Laravel version and Test framework
- Language
- English
- Price
- Free, no sign-up
- Updated
- Nov 7, 2026
Frequently asked questions
Why use an action class instead of putting everything in the controller?
The logic becomes reusable from commands, jobs and tests, and the controller only translates HTTP. It is also easier to test in isolation.
How do I catch N+1 queries in the generated code?
Enable `Model::preventLazyLoading()` outside production: Laravel throws an exception whenever a relationship is lazy-loaded.
What is this prompt for?
Build a Laravel feature end to end: migration, model, Form Request, Policy, controller, routes and Pest or PHPUnit feature tests, with no N+1 queries.
Which AI tools does it work with?
It is designed for ChatGPT, Claude, Gemini and Microsoft Copilot. We have not verified it in these tools yet, so results may vary between them.
How do I customize the prompt?
Click “Customize” and fill in the variables (Feature, Entities and relationships, Roles and permissions, Laravel version and Test framework). The text updates instantly and you can then copy it in one click. Values are stored only in your browser.
Do I need experience to use it?
It is intermediate: it helps to know the topic so you can provide good context and review the result critically.
Is this prompt free?
Yes. Every HomeCasa Prompts prompt is free, with no sign-up or subscription. You can also use them in professional projects.
Can I trust the AI output?
Treat it as a high-quality draft: always check data, figures and important claims before publishing or making decisions.