As a web developer with years of experience, working with Laravel is still no joke! It is a long way of learning, but also it is fun and useful for anyone who is interested in web development. For those who don't know what Laravel is, “it is a web application framework with expressive, elegant syntax.”

There are some things one should know about using resource controllers in Laravel. In this blog, I intend to tell you what a resource controller is and what its actions are. I hope this blog helps you with web development. 

What is a Resource Controller in Laravel?

Backend developers like me construct standardized projects on a regular basis as part of our day-to-day work, particularly for CRUD operations. We give each of them their own specific tasks within our controller, and we also build routes for each, which can be a time-consuming and, at times, difficult process.

This procedure requires more of our time and effort, and consequently, it lengthens the amount of time it takes to produce any given product or complete any given job. CRUD operations are very common, and everyone here is familiar with them.

C = Create

R= Retrieve

U= Update

D= Delete

Laravel Controller provides a facility for generating a controller with all these CRUD operations prebuilt with a single line comment, and this controller is known as Resource Controller.

Here is the command for the Resource controller:

php artisan make: controller controllerName –resource 

Actions of Resource Controller

A resource controller provides us with a total of seven methods with different actions

These actions are:

  • index
  • create
  • store
  • show
  • edit
  • update
  • destroy

Initializing the resource controller in our routing file:

We can initialize the resource controller routes by using the below snippet in our web.php file, which is located in the routes folder of our Laravel project using the below snippet, i.e.,

use AppHttpControllerscontrollerName;

Route::resource(‘photos', controllerName::class);

Let's take a look at the seven actions that a resource controller provides to us: 

1. Index Method

Route Name: controllerName.index

Request Method: GET

The pages of all the records are displayed using this method. If you have a product page, for example, you will use the index method to retrieve all of the records and show them in the view in the appropriate order.

image 31

2. Create Method

Route Name: controllerName.create

Request Method : GET

This method is used to display the form for creating the record in the table. For example, if you have a product table, we will create a form for the user to use to enter values for the products in the database, and then we will display that form to the user.

image 32

3. Store Method

Route Name: controllerName.store

Request Method: POST

This method is used to store the record in the table. For example, if you have a product table, then insert values in the products table by sending the params in a POST request in the route.

image 33

4. Edit Method

Route Name: controllerName.edit

Request Method: GET

This method is used to show the editing form for a specified resource. This function takes the id as an argument. It can be the primary key or any unique key of the table.

image 34

5. Show Method

Route Name: controllerName.show

Request Method: GET

Used to show the results for the specified resource from the database.

image 35

6. Update Method

Route Name: controllerName.update

Request Method: PUT/PATCH

Now we use that method to update the specified resource. This method takes arguments by following the PUT or PATCH as a request method followed by a unique id of the record we want to update.

image 36

7. Destroy Method

Route Name: controllerName.delete

Request Method: DELETE

Whenever we create a record, we also want a method and route for deleting this record. By using the destroy method of the resource controller, we can do the same. It takes the id of the table as an identifier for which record you want to delete.

image 37 1

After initializing your resource controller in the routes file, you can check your listed routes by command:

php artisan route:list

The outcome in your terminal will be:

image 38 1

8. Initializing the Specified Methods

When we initialize the resource controller routes, we can also define only the required routes. For example, if we need only the index and show method, then we can initialize the routes for only these two methods. If our controller name is PhotoController, then the code for doing the same is:

image 39 1

9. Defining API Resource Routes 

We can also define the resource routes of these resource controllers for the APIs. For this, we will follow the below snippet : 

image 40 1

So, these are the main aspects of a resource controller.

Wrapping Up

And yes, it's it's a wrap! I hope you understand what resource controllers are and also their actions. Using Laravel is fun when you know what you are doing. You can make the most out of it by understanding it better. 

There's a lot to learn, explore, understand and use in web development. I hope this blog added to your knowledge. 

For web design and web development services, you can count on webdew! Our expert team can get you the best services possible. Visit www.webdew.com to know more! Or even better, contact us!

                                                                                                                                Editor: Amrutha

Frequently Asked Questions

In Laravel, a “resource” and a “controller” serve different purposes:

  • Resource: In the context of routes, a resource is a way to define a group of routes for a controller that handles typical CRUD (Create, Read, Update, Delete) functions on a resource, such as a database table.
  • Controller: It is a PHP class responsible for handling incoming HTTP requests and controlling the application's logic. A controller contains methods that correspond to different actions or routes, and it interacts with models and views to generate responses.

In Laravel, a resource route is a way to define a group of routes for a controller that handles typical CRUD (Create, Read, Update, Delete) operations on a resource, such as a database table. It simplifies the definition of these routes and adheres to RESTful conventions.

To use a resource controller in Laravel 8:

  • Create a controller: Use the php artisan make: controller command to generate a resource controller, specifying the –resource flag.
  • Define routes: In your web.php or api.php file, use the Route::resource() method to define resource routes that map to the controller's actions.
  • Implement controller methods: In the generated controller, implement methods like index(), create(), store(), show(), edit(), update(), and destroy() to handle CRUD operations.
  • Views: Create view files for each controller action if needed.
  • Access routes: You can access the routes like /resource (index), /resource/create (create), /resource/{id} (show), etc., where “resource” is the name of the resource you defined.

The function of a resource controller in Laravel is to handle typical CRUD (Create, Read, Update, Delete) operations on a resource, such as a database table, by defining and implementing methods that correspond to these actions. It simplifies the routing and handling of these operations, following RESTful conventions.