Laravel will let you put anything anywhere. A controller can validate input, run three queries, apply a discount rule, send an email and return a view, and the framework will not object. The application works on day one. The trouble arrives a year later, when the discount rule has to change and nobody is sure how many places it lives in.

Structure is the answer to one question: when this rule changes, where do I go? This article is about the handful of conventions that keep that answer short.

Start with the request's journey

Every web request in a Laravel application passes through the same stages. Giving each stage one job is most of the work.

Stage Its one job Laravel tool
Entry Decide whether the caller may do this at all Middleware, gates, policies
Input Reject anything malformed Form requests
Decision Apply the business rule A service or action class
Persistence Read and write records Eloquent models
Output Shape what goes back API resources or views

When a stage does two jobs, the code for one of them becomes hard to find. A controller that validates and decides and formats is the usual example.

Validate at the edge

A form request moves validation out of the controller and gives it a name:

class StoreOrderRequest extends FormRequest
{
    public function authorize(): bool
    {
        return $this->user()->can('create', Order::class);
    }

    public function rules(): array
    {
        return [
            'customer_id' => ['required', 'integer', 'exists:customers,id'],
            'items' => ['required', 'array', 'min:1'],
            'items.*.product_id' => ['required', 'integer', 'exists:products,id'],
            'items.*.quantity' => ['required', 'integer', 'min:1', 'max:999'],
        ];
    }
}

The benefit is not tidiness. It is a guarantee: by the time the controller method runs, the input has the right shape. Everything after that point can stop defending itself against missing keys and wrong types. Use $request->validated() or $request->safe() rather than $request->all(), so that a field nobody validated can never reach the database.

Keep controllers thin

A controller's job is translation: turn an HTTP request into a call, and the result into an HTTP response.

class OrderController extends Controller
{
    public function store(StoreOrderRequest $request, PlaceOrder $placeOrder): JsonResponse
    {
        $order = $placeOrder->handle($request->user(), $request->validated());

        return OrderResource::make($order)->response()->setStatusCode(201);
    }
}

Three lines, and none of them contains a business rule. If the same order can later be placed from an import script or a queued job, PlaceOrder is called from there too, and the rules come along with it.

Put decisions in one place

The class that does the work goes by many names — service, action, use case. The name matters less than the rule: one class per thing the business does.

class PlaceOrder
{
    public function __construct(private StockLedger $stock) {}

    public function handle(User $user, array $data): Order
    {
        return DB::transaction(function () use ($user, $data) {
            $order = Order::create([
                'customer_id' => $data['customer_id'],
                'placed_by' => $user->id,
                'status' => OrderStatus::Pending,
            ]);

            foreach ($data['items'] as $item) {
                $product = Product::lockForUpdate()->findOrFail($item['product_id']);

                $this->stock->reserve($product, $item['quantity'], $order);

                $order->items()->create([
                    'product_id' => $product->id,
                    'quantity' => $item['quantity'],
                    'unit_price' => $product->price,
                ]);
            }

            return $order->refresh();
        });
    }
}

Notice what this class does not know: nothing about HTTP, nothing about JSON, nothing about which screen the request came from. That is what makes it testable without booting a browser, and reusable without copying.

Two details are worth pointing out. The whole operation is in a transaction, so a failure halfway leaves nothing behind. And the unit price is read from the product, not from the input — a client is never the authority on what something costs.

Let models be models

Eloquent models are a good home for three things: relationships, casts, and query scopes that give a name to a condition used in more than one place.

class Order extends Model
{
    protected function casts(): array
    {
        return ['status' => OrderStatus::class, 'placed_at' => 'datetime'];
    }

    public function items(): HasMany
    {
        return $this->hasMany(OrderItem::class);
    }

    public function scopeOpen(Builder $query): void
    {
        $query->whereIn('status', [OrderStatus::Pending, OrderStatus::Processing]);
    }
}

Order::open()->count() now means the same thing on the dashboard, in the report and in the nightly job. When "open" gains a third status, one line changes.

What does not belong in a model is orchestration: sending mail, calling other services, deciding workflows. Model events that quietly do those things are convenient until the day an import creates ten thousand rows and sends ten thousand emails.

Shape output deliberately

An API resource is the output counterpart of a form request. It decides which fields leave the application and in what form:

class OrderResource extends JsonResource
{
    public function toArray(Request $request): array
    {
        return [
            'id' => $this->id,
            'status' => $this->status->value,
            'total' => $this->total,
            'placed_at' => $this->placed_at?->toIso8601String(),
            'items' => OrderItemResource::collection($this->whenLoaded('items')),
        ];
    }
}

Returning a model directly is quicker and leaks every column, including the ones added next month. A resource makes adding a field a decision rather than an accident.

Move slow work out of the request

Anything that can take more than a moment — sending email, generating a PDF, importing a file, calling another service — belongs in a queued job. The request records that the work is needed and returns; the job does it and can be retried if it fails.

GenerateInvoicePdf::dispatch($order)->afterCommit();

afterCommit() matters. Without it, a fast worker can pick the job up before the surrounding transaction has committed, look for the order, and find nothing.

What to skip

Structure has a cost, and not every pattern earns it.

  • Repositories over Eloquent. Wrapping every model in an interface mostly produces a second, worse query builder. Scopes cover the real need.
  • A class for every tiny thing. A controller that reads three rows and returns them does not need a service. Add the layer when a rule appears.
  • Deep folder trees. app/Actions/Orders/PlaceOrder.php is enough. Nobody enjoys five levels of namespace.

The framework's defaults are a structure already. The aim is to follow them consistently, and add only what a real requirement asks for.

A checklist to end on

Open any feature in a codebase and ask:

  1. Is every write preceded by a form request?
  2. Could the business rule be called from a console command without changes?
  3. Is each multi-step write inside a transaction?
  4. Does output go through a resource or a view, never a raw model?
  5. Is anything slow still running inside the request?

Five yeses and the application will still be pleasant to work on when the rules change — which they will.