How To Use Laravel Resource Controller

A Laravel resource controller makes it easier to build RESTful controllers around resources. For instance, you may wish to create a controller that manages “blog posts” stored by your application. Normally, you use a laravel resource controller to group common routes in one class. In this post, I will show you how to create a resource controller that we will, in the future, use to manage blog posts in a demo application. Let us do it!

laravel resource controller


In your routes.php, you would add routes like this:

Anatomy of a blog post

Typically, you would want to be able to do the following easily when dealing with blog posts: (CRUD)

  • Create blog posts
  • Read blog posts from DB
  • Update blog posts
  • Delete blog posts at will

A laravel resource controller enables you to do all these in the same class and a single line in your routes.php file. I will be showing you how easy it is to do that today.

How to create a laravel resource controller

Open your terminal, navigate to the root of your laravel project and run this command:

Once you are done, open your routes.php file and simply add the following line to register a resourceful route to our brand new controller;

The beautiful thing about a laravel resource controller is that you do not have to tell it which routes are post, get or delete. It is smart enough to know what to do when you hit certain routes on your browser. Awesome right? Here is your generated resource controller.

That is exactly how you create a laravel resource controller; very easy to say the least. If you had your Post model (next blog post I promise) and your views, you would have a working blogging platform. The above controller uses the logged-in user to get their first name for use as the author of the blog post. Again, more stuff I have to write about.

That is all I will write today and if you have any questions regarding a laravel resource controller, please let me know through the comments or contact me.

Also, please consider sharing this post with your friends using the buttons below! Thank you and stay awesome!

Written By Elisha Chirchir

Elisha Chirchir is a software developer. He is also the founder of Simple Developer and co-founder of Instinctive Software Solutions. On any given day, he works on both Android and Web Development. During his 'free time', he offers training to those interested in learning how to code in php, java, python, javaScript etc. You can easily find him on StackOverflow Android chatroom or on Twitter @Eenvincible

One Comment on “How To Use Laravel Resource Controller

  1. […] How To Use Laravel Resource Controller Laravel resource controller is the easiest way to create a CRUD application in the highly popular Laravel framework. In this post, I show you how to use it. […]

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.