com um clique
laravel-iterating-on-code
// Refine AI-generated code through specific feedback—point out errors, identify gaps, show desired changes, reference style guides
// Refine AI-generated code through specific feedback—point out errors, identify gaps, show desired changes, reference style guides
Request effective code reviews—specify focus areas, provide context, ask for architectural feedback, reference Laravel conventions
Create effective debugging prompts—include error messages, stack traces, expected vs actual behavior, logs, and attempted solutions
Provide comprehensive context in prompts—files, errors, Laravel version, dependencies, and monorepo details—for accurate AI responses
Use Laravel-specific vocabulary—Eloquent patterns, Form Requests, API resources, jobs/queues—to get idiomatic framework code
Structure prompts for clarity—separate concerns, prioritize requests, specify acceptance criteria, and break work into testable increments
Define clear constraints—performance, security, testing, architecture, dependencies—so AI generates code that meets your project standards
| name | laravel:iterating-on-code |
| description | Refine AI-generated code through specific feedback—point out errors, identify gaps, show desired changes, reference style guides |
AI-generated code rarely perfect on first try. Effective iteration gets you from "close" to "exactly right."
"This doesn't work"
"The validation is incorrect:
Current code:
'email' => 'required|email|unique:users'
Problem: This allows updating a user's email to another user's existing email. The unique rule needs to ignore the current user.
Fix:
'email' => [
'required',
'email',
Rule::unique('users')->ignore($this->user()->id)
]
This allows the user to keep their own email but prevents duplicates with other users."
Why it works: Identifies the specific problem, explains why it's wrong, shows the correct solution.
"Something's missing"
"The OrderService is missing error handling:
Current implementation:
public function createOrder(array $data): Order
{
$order = Order::create($data);
$this->processPayment($order);
return $order;
}
Missing:
Add:
DB::transaction(function () use ($data) {
$this->validateInventory($data['items']);
$order = Order::create($data);
$this->processPayment($order);
event(new OrderCreated($order));
return $order;
});
Plus add try/catch for payment failures."
Why it works: Lists specific missing pieces with context and shows how to add them.
"Make it better"
"Refine the query for better performance:
Current:
$products = Product::all()->filter(function ($product) {
return $product->isActive() && $product->inStock();
});
Issues:
Refined:
$products = Product::query()
->where('active', true)
->where('stock_quantity', '>', 0)
->get();
Move filtering to database, use indexed columns, avoid loading unnecessary data."
Why it works: Explains what needs refinement and why, shows the improved version.
"Follow our style guide"
"Update to match our coding standards:
Current:
public function get_user_orders($userId) {
return Order::where('user_id', $userId)->get();
}
Style issues:
getUserOrders$userId ✓ (already correct)Corrected:
/**
* Get all orders for a specific user.
*/
public function getUserOrders(int $userId): Collection
{
return Order::where('user_id', $userId)->get();
}
See our style guide: docs/coding-standards.md"
Why it works: Points to specific style violations, shows corrections, references the style guide.
"Change the validation, add error handling, refactor the service, update the tests, and add logging"
"Let's iterate step by step:
Step 1: Fix the validation issue first
'email' => Rule::unique('users')->ignore($this->user()->id)
Let's verify this works before moving on."
[After validation confirmed working]
"Step 2: Now add error handling for the payment processing
try {
$this->processPayment($order);
} catch (PaymentException $e) {
Log::error('Payment failed', ['order' => $order->id]);
throw new OrderProcessingException('Payment failed', previous: $e);
}
Test this before we continue."
Why it works: One change at a time, validate each step, build confidence incrementally.
"The relationship is incorrect:
**Current:** `return $this->hasMany(Post::class);`
**Problem:** A User has many Posts, but you're defining this in the Post model. This creates a circular relationship.
**Fix:** Move this to the User model, or if you meant Post belongs to User:
```php
// In Post model
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
```"
"Missing authorization check:
**Why it matters:** Any authenticated user can delete any order, not just their own.
**Add this to OrderController@destroy:**
```php
$this->authorize('delete', $order);
And create the policy method:
// In OrderPolicy
public function delete(User $user, Order $order): bool
{
return $user->id === $order->user_id;
}
```"
"Current implementation has issues:
**Current:**
```php
foreach ($orders as $order) {
$order->load('items', 'customer', 'shipping');
}
Issues:
Improved:
$orders = Order::with(['items', 'customer', 'shipping'])->get();
Single query with eager loading."
## Quick Reference
Iterate effectively:
- **Be specific** - Point to exact lines, explain exact problems
- **Show, don't just tell** - Provide corrected code
- **Explain why** - Help the AI understand the reasoning
- **One change at a time** - Validate incrementally
- **Reference standards** - Point to style guides, docs, examples
Specific feedback = better iterations = code that fits your needs.