Skip to main content

Overview

Every route or controller action must return a response to the user’s browser. Laravel makes it easy to return strings, arrays, views, redirects, and JSON — all with minimal code.

Basic responses

Returning a string

The simplest response is a plain string. Laravel automatically converts it into a proper HTTP response:

Returning an array

Return an array and Laravel automatically converts it to a JSON response:
Returning an Eloquent model or collection also produces a JSON response automatically — a convenient pattern for building simple APIs.

Returning an Eloquent model

The Response object

Use the response() helper when you need to control the HTTP status code or headers:
The first argument is the response body; the second is the HTTP status code.

Common HTTP status codes

Returning a view

Use view() to render a Blade template. This is the most common response in a web application:
The first argument is the view name; the second is an array of data to pass to the template.
Adding the Illuminate\View\View return type hint makes the intent of your method immediately clear.

Controlling status code and headers alongside a view

Redirects

Basic redirect

The redirect() helper returns a redirect response. It’s commonly used after form submissions to send users to a different page:
Controller example:

Redirecting to a named route

Use the route() helper to redirect by route name instead of a hard-coded URL. If the URL ever changes, you only need to update the route definition:

Redirecting back

Use back() to redirect to the user’s previous location. This is common after a validation failure:

Redirect with a flash message

Use with() to store a message in the session for the next request:
Display the message in a Blade template:

JSON responses

For APIs, return JSON explicitly with response()->json(). Laravel sets the Content-Type: application/json header automatically:
Include a status code:
Controller example:
Returning an array or Eloquent model directly also produces JSON, but response()->json() gives you fine-grained control over status codes and headers.

Response headers

Add headers with the header() method:
Set multiple headers at once with withHeaders():

Example: a complete resource controller

1

Define the routes

2

Implement the controller

In web applications, display actions return a View and mutating actions (create, update, delete) redirect after completing. This pattern prevents duplicate form submissions on refresh.

Next steps

Session

Learn how to store data across requests using Laravel’s session.
Last modified on March 29, 2026