Laravel Controllers with resource

php artisan make:controller BookController –resource. What is this resource at end of artisan command? Let’s break it down in simple terms.

If you’re learning Laravel, you’ve probably seen these two commands:

php artisan make:controller BookController

and

php artisan make:controller BookController --resource

At first, they look similar—but they serve different purposes. Let’s break it down in simple terms.


📌 What is a Controller?

In Laravel, a controller is a file where you write logic for handling user requests.

For example:

  • Show a list of books
  • Add a new book
  • Edit a book
  • Delete a book

🧱 Option 1: Basic Controller

php artisan make:controller BookController

This creates an empty controller:

class BookController extends Controller
{
    //
}

👉 You have to:

  • Write all functions yourself
  • Define routes manually

✔️ Use this when:

  • You need custom logic
  • You are not building a standard CRUD app

⚡ Option 2: Resource Controller

php artisan make:controller BookController --resource

This creates a controller with 7 ready-made methods:

public function index() {}
public function create() {}
public function store() {}
public function show($id) {}
public function edit($id) {}
public function update($id) {}
public function destroy($id) {}

These methods follow a standard pattern called CRUD:

ActionMethod
Show all dataindex
Show form to createcreate
Save new datastore
Show one itemshow
Show edit formedit
Update dataupdate
Delete datadestroy

🔗 The Magic of Route::resource

Instead of writing many routes, Laravel gives you a shortcut:

Route::resource('books', BookController::class);

This single line creates all these routes:

MethodURLController Method
GET/booksindex
GET/books/createcreate
POST/booksstore
GET/books/{id}show
GET/books/{id}/editedit
PUT/PATCH/books/{id}update
DELETE/books/{id}destroy

🧠 Simple Example

Let’s say you are building a Book Management App:

  • View all books → index()
  • Add a book → create() + store()
  • Edit a book → edit() + update()
  • Delete a book → destroy()

With a resource controller, everything is already structured for you 🚀


🤔 Which One Should You Use?

Use Basic Controller if:

  • You need custom features
  • Your app is not CRUD-based

Use Resource Controller if:

  • You are building CRUD (most common case)
  • You want faster development
  • You want clean and organized code

💡 Beginner Tip

If you’re just starting with PHP and Laravel:

👉 Start with --resource
It helps you learn structure and saves time.


🎯 Final Thoughts

  • Basic Controller = more control, more work
  • Resource Controller = faster, structured, beginner-friendly

Most real-world Laravel apps use resource controllers because they follow best practices.