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
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:
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.
Let's take a look at the seven actions that a resource controller provides to us:
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.
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.
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 POST request in the route.
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.
Route Name: controllerName.show
Request Method: GET
Used to show the results for the specified resource from the database.
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.
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.
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:
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:
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 :
So these are the main aspects of a resource controller.
And yes, its it's a wrap! I hope you understood 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.