webdew-logo-new_v2-1-1
   
webdew-logo-new_v2-1
Close
  • Services
  • Work
  • Resources
    • Knowledge
    • Ebooks
    • Tools
      • Website Audit
      • Website Grader
      • HubSpot ROI
  • Blog
  • About
Let's talk

  • fb-icon-01
  • tw-icon-01
  • insta-icon-01 (1)
  • linkedin-icon-01
  • youtube-icon-01
website-green-icon
Websites
  • Website Designing
  • Website Development
  • Website Migration
  • HubSpot Website
  • WordPress Website
  • Growth-Driven Design
video-blue-icon
Videos
  • 3D Videos
  • 2D Animation
  • Whiteboard Animation
  • Video Ads
  • Explainer Videos
  • Testimonial Videos
  • Onboarding Videos
  • Tutorial Videos
  • Social Media Videos
  • Product Demo Videos
marketing-yellow-icon
Marketing
  • Inbound Marketing
  • Inbound Consulting
  • Marketing Automation
  • Search Engine Optimization
  • Brand Domains
hubspot-red-icon
HubSpot
  • HubSpot Apps
  • HubSpot Themes
  • HubSpot Migration
  • HubSpot Onboarding
  • HubSpot Management
  • HubSpot Development
  • HubSpot Audit
  • Free HubSpot Demo
  • HubSpot Diamond Partner
webdew-logo-new_v2-1-1
  • Services
  • Work
  • Resources
    • Knowledge
    • Ebooks
    • Tools
      • Website Audit
      • Website Grader
      • HubSpot ROI
  • Blog
  • About
Let's talk

What is a resource controller in laravel and what are its actions?

What is a resource controller in laravel and what are its actions?-vbh
Tag: Websites
Author: Bhupendra Kumar
Bhupendra Kumar
Quick Links

Subscribe to Our Blog

Never Miss Any Update From Us !

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."

8 Best image optimization tools for your web app

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 App\Http\Controllers\controllerName;

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

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

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.

Index method

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.

create method

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 POST request in the route.

store method

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.

edit method

Show method

Route Name: controllerName.show

Request Method: GET

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

show method

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.

 

update method

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.

destroy method

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:

outcome

A complete guide that will help you protect websites data

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:

Initializing the specified methods

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 : 

Defining API resource routes 

So these are the main aspects of a resource controller.

Wrapping up

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. 

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

improve your website usability and UX

Related Blogs

What is OOPs concept? How can you create a crud operation?-vbh
Websites

What is OOPs concept? How can you create a crud operation?

What is the REST API in laravel: Everything you need to know-vbh
Websites

What is the REST API in laravel: Everything you need to know

What is a 404 page and how can you create one for your website?-vbh
Websites

What is a 404 page and how can you create one for your website?

review-star

Based on 1000+ Reviews on

footer-dot-logo-img
  • Hubspot_Logo-icon
  • clutch_Logo-icon
  • upwork_Logo-icon
  • designrush_Logo-icon
  • goodfirms_Logo-icon
  • g2_Logo-icon
WE ARE GLOBAL
  • US-flag
  • CA-flag
  • IN-flag
  • GB-flag

Copyright © 2019 - 2021 Webdew, Inc. All Rights Reserved

Copyright © 2019 - 2021 Webdew Labs Inc. All Rights Reserved

Copyright © 2016 - 2021 Webdew Private Limited. All Rights Reserved

Copyright © 2019 - 2021 Webdew, Inc. All Rights Reserved

WE ARE SOCIAL
  • fb-icon-01
  • tw-icon-01
  • insta-icon-01 (1)
  • linkedin-icon-01
  • youtube-icon-01
Terms of Services
Privacy Policy