MasterPHP.in
Try Compiler →
Laravel Tutorial

Resource Routes & route:list in Laravel


Say you are building an invoice app. Invoices need a list page, a form to create one, a page to save it, a detail page, an edit form, an update action and a delete action. That is seven routes for one feature. Now add clients, payments and orders, and you are writing close to thirty routes that all follow the same pattern.

Typing them by hand is slow, and it is easy to make small mistakes: a wrong verb, a missing name, a typo in a URL. Worse, after a few weeks of adding features, nobody remembers which routes actually exist in the project.

Laravel gives you two tools for this. Resource routes register all the standard routes for a feature with a single line. route:list is an Artisan command that prints every route in your application, so you can always see what is really there.

 

The Seven Routes Almost Every Module Needs

Most features in a web app need the same set of actions, often called CRUD (create, read, update, delete). Here is how they map to HTTP verbs and URLs for invoices:

GET        /invoices                  → list all invoices
GET        /invoices/create           → show the "new invoice" form
POST       /invoices                  → save a new invoice
GET        /invoices/{invoice}        → show one invoice
GET        /invoices/{invoice}/edit   → show the edit form
PUT/PATCH  /invoices/{invoice}        → save the changes
DELETE     /invoices/{invoice}        → delete the invoice

Writing these by hand looks like this:

// routes/web.php
use App\Http\Controllers\InvoiceController;
use Illuminate\Support\Facades\Route;

Route::get('/invoices', [InvoiceController::class, 'index'])->name('invoices.index');
Route::get('/invoices/create', [InvoiceController::class, 'create'])->name('invoices.create');
Route::post('/invoices', [InvoiceController::class, 'store'])->name('invoices.store');
Route::get('/invoices/{invoice}', [InvoiceController::class, 'show'])->name('invoices.show');
Route::get('/invoices/{invoice}/edit', [InvoiceController::class, 'edit'])->name('invoices.edit');
Route::put('/invoices/{invoice}', [InvoiceController::class, 'update'])->name('invoices.update');
Route::delete('/invoices/{invoice}', [InvoiceController::class, 'destroy'])->name('invoices.destroy');

This works, but it is seven lines of repeated work, and you have to repeat it for every feature. You also named these routes by hand in the previous lessons, following the resource.action pattern. Laravel can do all of that for you.

 

Route::resource: Seven Routes in One Line

First, you need a controller. Laravel can generate one with all seven methods already stubbed out:

php artisan make:controller InvoiceController --resource

You will learn what goes inside these methods in the Resource Controllers lesson. For now, the controller only needs to exist. Now replace all seven lines above with one:

// routes/web.php
use App\Http\Controllers\InvoiceController;
use Illuminate\Support\Facades\Route;

Route::resource('invoices', InvoiceController::class);

That single line registers the same seven routes, with the same URLs, the same HTTP verbs and the same names.

 

What Laravel Generates

Verb        URI                        Action     Route Name
GET|HEAD    /invoices                  index      invoices.index
GET|HEAD    /invoices/create           create     invoices.create
POST        /invoices                  store      invoices.store
GET|HEAD    /invoices/{invoice}        show       invoices.show
GET|HEAD    /invoices/{invoice}/edit   edit       invoices.edit
PUT|PATCH   /invoices/{invoice}        update     invoices.update
DELETE      /invoices/{invoice}        destroy    invoices.destroy

Two details are worth noticing. The parameter name is the singular form of the resource name, so invoices becomes {invoice}. This is what makes it work smoothly with route model binding, which you learned about earlier in this chapter. And the route names follow the convention from the Named Routes lesson, so route('invoices.show', ['invoice' => 1042]) works without any extra setup.

 

Why Forms Need Method Spoofing

HTML forms can only send GET and POST. They cannot send PUT, PATCH or DELETE. So for update and delete forms, you send a POST request with a hidden field that tells Laravel which verb you really mean:

<form method="POST" action="{{ route('invoices.destroy', ['invoice' => 1042]) }}">
    @csrf
    @method('DELETE')
    <button type="submit">Delete Invoice</button>
</form>

The @method('DELETE') line is called method spoofing. It is explained fully in the Forms & Validation chapter, but remember it now, because it causes a common error at the end of this lesson.

 

Reading Your Routes with route:list

As your project grows, you need a quick way to answer questions like “Does this route exist?” or “What is its name?” The route:list command answers both.

 

Basic Usage

php artisan route:list

A typical output for the invoice resource looks like this:

  GET|HEAD        invoices ................ invoices.index › InvoiceController@index
  POST            invoices ................ invoices.store › InvoiceController@store
  GET|HEAD        invoices/create ......... invoices.create › InvoiceController@create
  GET|HEAD        invoices/{invoice} ...... invoices.show › InvoiceController@show
  PUT|PATCH       invoices/{invoice} ...... invoices.update › InvoiceController@update
  DELETE          invoices/{invoice} ...... invoices.destroy › InvoiceController@destroy
  GET|HEAD        invoices/{invoice}/edit . invoices.edit › InvoiceController@edit

                                                       Showing [7] routes

Read each line from left to right: the HTTP verbs, the URI, the route name, and finally the controller and method that will run. The exact order and spacing may differ a little on your machine. Your app will also list a few built-in routes of its own, such as the home page and a storage route.

 

Filtering the Output

In a real project the full list can run to hundreds of lines. Use filters to find what you need:

# Only routes whose name contains "invoices"
php artisan route:list --name=invoices

# Only routes whose URI starts with "invoices"
php artisan route:list --path=invoices

# Only routes that respond to a given verb
php artisan route:list --method=POST

# Hide routes added by third-party packages
php artisan route:list --except-vendor

 

Seeing the Middleware

By default, middleware is not shown. Add -v to display the middleware attached to each route, or -vv to also expand middleware groups into the individual middleware they contain.

php artisan route:list -v
php artisan route:list -vv

Make route:list a habit. Whenever a URL gives you a 404 or 405 error, run it first. It shows you the truth about what Laravel has registered, which is often different from what you remember writing.

 

Limiting Routes with only() and except()

Not every feature needs all seven routes. An invoice should probably never be deleted, because accounting records must stay. A payment record created by Razorpay is something you only view, never edit by hand.

Registering routes you do not need is wasteful, and it also leaves unused endpoints open in your application. Use only() to list exactly what you want, or except() to remove a few:

// Payments are read-only: list and detail pages only
Route::resource('payments', PaymentController::class)->only(['index', 'show']);

// Invoices can be created, viewed and edited, but never deleted
Route::resource('invoices', InvoiceController::class)->except(['destroy']);

After this change, route:list --name=payments shows only two routes, and a DELETE request to /invoices/1042 returns a 405 error. Choose only() when you need a few routes, and except() when you need almost all of them.

 

API Resources with apiResource()

When you build a REST API, there are no HTML pages, so the create and edit form routes make no sense. Route::apiResource() registers only the five routes an API needs:

Route::apiResource('orders', OrderController::class);


GET|HEAD    /orders             index      orders.index
POST        /orders             store      orders.store
GET|HEAD    /orders/{order}     show       orders.show
PUT|PATCH   /orders/{order}     update     orders.update
DELETE      /orders/{order}     destroy    orders.destroy

A new Laravel project has no routes/api.php file. You add it with php artisan install:api, and API routes live there. That setup is covered in the Building REST APIs chapter, so do not worry about it yet. For now, just remember that apiResource is the same idea without the two form routes.

 

Customizing Resource Routes

The defaults are right most of the time, but you can change them when needed.

 

Changing Route Names

Route::resource('invoices', InvoiceController::class)->names([
    'index' => 'billing.invoices.list',
]);

Now route('billing.invoices.list') points to the invoice list. Only the index name changes, and the other six keep their defaults.

 

Changing the Parameter Name

Route::resource('invoices', InvoiceController::class)->parameters([
    'invoices' => 'invoice_number',
]);

// URI becomes: /invoices/{invoice_number}

Change this only when you have a good reason. The default singular name works best with model binding.

 

Nested Resources

Invoices belong to clients. You may want URLs that show this relationship, like /clients/7/invoices/1042. Use a dot between the two resource names:

Route::resource('clients.invoices', ClientInvoiceController::class);


GET|HEAD    /clients/{client}/invoices                    clients.invoices.index
POST        /clients/{client}/invoices                    clients.invoices.store
GET|HEAD    /clients/{client}/invoices/create             clients.invoices.create
GET|HEAD    /clients/{client}/invoices/{invoice}          clients.invoices.show
GET|HEAD    /clients/{client}/invoices/{invoice}/edit     clients.invoices.edit
PUT|PATCH   /clients/{client}/invoices/{invoice}          clients.invoices.update
DELETE      /clients/{client}/invoices/{invoice}          clients.invoices.destroy

Long URLs like these get awkward. An invoice number is already unique, so the client id in the URL is not needed to find, edit or delete one. Add shallow() and only the routes that really need the parent keep it:


Route::resource('clients.invoices', ClientInvoiceController::class)->shallow();

With shallow(), index, create and store keep /clients/{client}/invoices, while show, edit, update and destroy become /invoices/{invoice}. How Laravel finds an invoice that belongs to a given client is part of Eloquent relationships, which you will meet in a later chapter.

 

Registering Several Resources at Once

If you have many resources, Route::resources() takes an array:

Route::resources([
    'invoices' => InvoiceController::class,
    'clients'  => ClientController::class,
    'payments' => PaymentController::class,
]);

This registers all seven routes for each one. You cannot use only() or except() with it, so use separate Route::resource() lines when you need to limit routes. Putting several routes under a shared prefix or middleware is done with route groups, which you learned in the previous lessons.

 

Common Mistakes

Mistake 1: Adding a Custom Route After the Resource

Suppose you add an export route for invoices below the resource line:

Route::resource('invoices', InvoiceController::class);

Route::get('/invoices/export', [InvoiceController::class, 'export']);

Visit /invoices/export and Laravel matches the show route first, treating the word export as an invoice id. With route model binding you will see:

404 Not Found
No query results for model [App\Models\Invoice] export

Routes match from top to bottom, and the first match wins. The fix is to define your custom route before the resource:

Route::get('/invoices/export', [InvoiceController::class, 'export'])->name('invoices.export');

Route::resource('invoices', InvoiceController::class);

 

Mistake 2: Forgetting to Import the Controller

Route::resource('invoices', InvoiceController::class);
// no "use App\Http\Controllers\InvoiceController;" at the top of the file


Illuminate\Contracts\Container\BindingResolutionException
Target class [InvoiceController] does not exist.

Laravel does not guess the controller namespace. Add the use line at the top of routes/web.php.

 

Mistake 3: A Missing Controller Method

If you limit routes with except(), you do not need those methods. But if a route is registered and its method is missing, you get:

BadMethodCallException
Method App\Http\Controllers\InvoiceController::update does not exist.

Generating the controller with --resource avoids this, because all seven methods are created for you.

 

Mistake 4: Deleting Without Method Spoofing

<form method="POST" action="{{ route('invoices.destroy', ['invoice' => 1042]) }}">
    @csrf
    <button type="submit">Delete</button>
</form>


Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException
The POST method is not supported for route invoices/1042. Supported methods: GET, HEAD, PUT, PATCH, DELETE.

The browser sent a plain POST, but the route expects DELETE. Add @method('DELETE') inside the form.

 

Mistake 5: Registering Routes You Never Use

Writing Route::resource('payments', PaymentController::class) and implementing only two methods leaves five live routes that either crash or, worse, do something you did not plan. Always limit the routes with only() or except(), then confirm the result with route:list.

 

Quick Summary

  • Route::resource('invoices', InvoiceController::class) registers seven standard routes in one line
  • Create the controller with php artisan make:controller InvoiceController --resource
  • Route names follow invoices.index, invoices.show and so on, and the parameter is the singular name like {invoice}
  • HTML forms need @method('PUT') or @method('DELETE') to reach update and destroy
  • Use only() and except() to register just the routes you need
  • Route::apiResource() skips create and edit, because APIs have no forms
  • Use dots for nested resources like clients.invoices, and shallow() to keep URLs short
  • Define custom routes before the resource line, or the {invoice} route will catch them
  • php artisan route:list shows every route, and --name, --path, --method and -v narrow or expand the output

 

What’s Next

You can now register a full set of routes quickly and inspect them whenever you need. The last routing lesson, Redirect, View & Fallback Routes, covers three special route types: routes that only redirect, routes that only return a view, and a fallback route that handles every URL no other route matched.

Frequently Asked Questions

Route::resource registers seven routes, including create and edit, which show HTML forms. Route::apiResource registers only five routes and leaves out those two, because an API does not return forms. Use resource for web pages and apiResource for JSON APIs.
PUT is meant for replacing a whole record, and PATCH is meant for changing only some fields. Laravel registers both on the same URL, so either one reaches your update method. In practice, most developers use PUT in forms and never think about the difference.
Run php artisan route:list in your project folder. It prints the HTTP verb, URI, route name and controller for every route. You can add --name, --path or --method to filter the list, and -v to show middleware.
Yes. Define the extra route in the same routes file, but place it above the Route::resource line. Routes are matched from top to bottom, so a route like /invoices/export must come before /invoices/{invoice}, or the resource route will catch it first.
Yes. Route::resource always points to a controller class, and each generated route calls one of its methods. If you do not want a controller, define simple routes with closures instead. For anything with real CRUD logic, a controller is the better choice.

Share this tutorial