Laravel apiResource vs resource: Understanding the Functional Difference

Route::apiResource() automatically omits the create and edit routes, generating only the five actions needed for stateless JSON APIs, while Route::resource() creates all seven RESTful routes including the HTML form handlers.

When building applications with the laravel/framework repository, choosing between apiResource and resource determines which RESTful actions get registered for your controllers. This distinction is crucial for API development, as it prevents unnecessary routes that expect HTML responses from cluttering your route table.

What is Route::resource in Laravel?

The Route::resource() method generates a complete set of RESTful routes designed for traditional web applications. According to the source code in src/Illuminate/Routing/ResourceRegistrar.php (lines 21-22), the default action list includes seven methods:

protected $resourceDefaults = ['index', 'create', 'store', 'show', 'edit', 'update', 'destroy'];

When you register a resource route:

use App\Http\Controllers\PostController;

Route::resource('posts', PostController::class);

Laravel creates the following routes:

  • GET /posts → index
  • GET /posts/create → create (returns HTML form)
  • POST /posts → store
  • GET /posts/{post} → show
  • GET /posts/{post}/edit → edit (returns HTML form)
  • PUT/PATCH /posts/{post} → update
  • DELETE /posts/{post} → destroy

The create and edit actions specifically serve HTML forms, making them unnecessary for JSON APIs.

What is Route::apiResource in Laravel?

The Route::apiResource() method is optimized for API development, automatically excluding the HTML-specific routes. As implemented in src/Illuminate/Routing/Router.php (lines 82-93), this method pre-filters the action list before delegating to the resource registrar:

public function apiResource($name, $controller, array $options = [])
{
    $only = ['index', 'show', 'store', 'update', 'destroy'];

    return $this->resource($name, $controller, array_merge(['only' => $only], $options));
}

When you use apiResource:

use App\Http\Controllers\Api\PostController;

Route::apiResource('posts', PostController::class);

Laravel generates only these five routes:

  • GET /posts → index
  • POST /posts → store
  • GET /posts/{post} → show
  • PUT/PATCH /posts/{post} → update
  • DELETE /posts/{post} → destroy

Key Differences Between resource and apiResource

The fundamental functional difference lies in the default action set each helper registers:

Feature Route::resource() Route::apiResource()
Actions Generated 7 (index, create, store, show, edit, update, destroy) 5 (index, store, show, update, destroy)
HTML Form Routes Includes /create and /{id}/edit Excludes form routes
Intended Use Traditional web apps with HTML views Stateless JSON APIs
Route Table Size Larger (7 entries) Smaller (5 entries)

Both methods ultimately use the same ResourceRegistrar class in src/Illuminate/Routing/ResourceRegistrar.php. The apiResource method simply injects an ['only' => [...]] constraint before registration, as seen in the Router.php implementation.

When to Use Each Approach

Web Applications with HTML Views

Use Route::resource() when building traditional server-rendered applications. The create and edit routes return HTML forms that users interact with in a browser. Without these routes, you would need to manually implement form display logic.

API Development and JSON Responses

Use Route::apiResource() when building RESTful APIs that return JSON. Since API clients typically don't need HTML forms (they construct requests programmatically), the create and edit routes serve no purpose. Using apiResource keeps your route table clean and prevents accidental exposure of form endpoints in your API.

Customizing Resource Routes

Both helpers accept the same customization options. You can override the defaults using only, except, or names:

// Exclude specific actions from apiResource
Route::apiResource('posts', PostController::class)->except(['destroy']);

// Add additional routes to a resource
Route::resource('posts', PostController::class)->only(['index', 'show']);

Because apiResource internally calls resource() with a pre-set only array, any additional options you provide are merged correctly in Router.php.

Summary

  • Route::resource() generates seven RESTful routes including create and edit for HTML forms, suitable for traditional web applications.
  • Route::apiResource() generates five API-specific routes, automatically excluding the HTML form routes to optimize for JSON-based APIs.
  • Both methods use the same underlying ResourceRegistrar class in src/Illuminate/Routing/ResourceRegistrar.php, but apiResource pre-filters actions in src/Illuminate/Routing/Router.php (lines 82-93).
  • Choose resource for server-rendered HTML views and apiResource for stateless API endpoints to maintain clean route definitions.

Frequently Asked Questions

Does apiResource create different route names?

No, apiResource uses the same naming convention as resource for the actions it generates. The route names for index, store, show, update, and destroy remain identical (e.g., posts.index, posts.store). The only difference is that create and edit names are never registered since those routes are omitted.

Can I use apiResource for web applications?

While technically possible, it is not recommended. If you use apiResource in a traditional web application, you lose the create and edit routes that typically return HTML forms. You would need to manually define additional routes to handle form displays, defeating the convenience of automated resource routing.

How do I add custom routes to an apiResource?

You can chain additional route definitions before or after the apiResource declaration, or use the only and except methods to customize the default set. For example:

Route::apiResource('posts', PostController::class);
Route::get('posts/{post}/stats', [PostController::class, 'stats']);

Since apiResource delegates to the underlying resource method in Router.php, it supports the same fluent interface for customization.

What Laravel version introduced apiResource?

The apiResource method has been available since Laravel 5.4, released in January 2017. It was introduced specifically to streamline API development by providing a convenient shorthand for the common pattern of excluding HTML form routes from resource controllers. The implementation in src/Illuminate/Routing/Router.php has remained consistent in its approach of filtering the $resourceDefaults array defined in ResourceRegistrar.php.

Have a question about this repo?

These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:

Share the following with your agent to get started:
curl -s "https://instagit.com/install.md"

Works with
Claude Codex Cursor VS Code OpenClaw Any MCP Client

Maintain an open-source project? Get it listed too →