Laravel Resource Controllers

Laravel resource routing assigns the “CRUD” routes to a controller with the help of single line code.

For E.g.,

If we wish to create a controller that handles all HTTP requests “photos” stored by our application using the make:controller Artisan command. We can quickly create a controller.

This above command will generate a controller at

For each of the available resource operations, the controller will contain a method.

In the next step, we are going to register an original route to the controller:

The single route declaration creates many routes to handle different types of action on the resource. The generated controller will already have methods for these actions, including notes informing us to the HTTP verbs and URIs they handle.

We register many resource controllers at once by passing an array to the resource method:

Actions Handled By Resource Controller

Verb URI Action Route Name
GET /photos index photos.index
GET /photos/create create photos.create
POST /photos store photos.store
GET /photos/{photo} show  photos.show
GET /photos/{photo}/edit edit photos.edit
PUT/PATCH /photos/{photo} update photos.update
DELETE /photo/{photo} destroy photos.destory

Specifying the Resource Model:

If we are using route model binding and would like the resource controllers methods to type-hint a model instance, we use the --model option when generating the controller:

Spoofing Form Methods:

Since the HTML forms cant make PUT, PATCH, or DELETE requests, we will need to add a hidden _method field to spoof these HTTP verbs. The @method Blade directive can create this field for us:

laravel resource controllers

The Resource Controllers are divided into 5 different types:

Partial Resource Route

When declaring a resource route, we specify a subset of actions.

The controller should handle instead of the face set of default actions:

API Resource Routes:

When declaring resource routes that will be consumed by APIs, we will commonly want to exclude routes which present HTML templates like create and edit.

We use the apiResource method to exclude these two routes automatically:

We register many API resource controllers at once by passing an array to the apiResources method:

For quickly generate an API resource controller which does not include the create or edit methods, we use the –api switch for executing the make:controller command:

Naming Resource Routes

By default, all type of resource controller actions have a route name. We can override these names by passing a names array with our options:

Naming Resource Route Parameters

The Route::resource will create the route parameters for our resource routes based on the “singularized” version of the resource name.

We can easily override this on resource basis by using the parameters method.

The array passed into the parameters method should be an associative array of resource names and parameters routes:

The above example generates the following URIs for the resource`s show route:

Localizing Resource URIs

The Route::resource is used by default; it will create a resource URIs using the English verbs.

If we need to localize the create and edit action verbs, we can use the Route::resourceVerbs method.

It may be done in the boot method of our AppServiceProvider.

Once the verbs have been customized, the resource route registration like Route::resource(‘fotos’, ‘PhotoController’) will produce the following URIs:

Supplementing Resource Controllers

If we need to add additional routes to a resource controller over the default set of resource routes, we should define these routes before our call to the Route::resource.

Otherwise, the routes defined by the resource method will take the lead over our supplement routes: