How to Create Resource Controller in Laravel Quickly
To create a resource controller in Laravel, use the
php artisan make:controller ControllerName --resource command. This generates a controller with predefined methods for CRUD operations following Laravel's conventions.Syntax
The basic syntax to create a resource controller is:
php artisan make:controller ControllerName --resource
Here, ControllerName is the name of your controller. The --resource flag tells Laravel to generate methods for common actions like index, create, store, show, edit, update, and destroy.
bash
php artisan make:controller PhotoController --resource
Example
This example creates a resource controller named PhotoController. It includes all the methods needed to manage photos in your app.
After running the command, you can find the controller in app/Http/Controllers/PhotoController.php with methods like index() to list photos and store() to save a new photo.
php
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; class PhotoController extends Controller { public function index() { // Show list of photos } public function create() { // Show form to create a photo } public function store(Request $request) { // Save new photo } public function show($id) { // Show a single photo } public function edit($id) { // Show form to edit photo } public function update(Request $request, $id) { // Update photo } public function destroy($id) { // Delete photo } }
Output
Controller file created at app/Http/Controllers/PhotoController.php with resource methods.
Common Pitfalls
Common mistakes when creating resource controllers include:
- Forgetting the
--resourceflag, which creates an empty controller without resource methods. - Not registering the resource controller in routes, so URLs don't work.
- Using inconsistent controller names that don't match Laravel naming conventions.
Always register your resource controller in routes/web.php like this:
Route::resource('photos', PhotoController::class);bash
php artisan make:controller PhotoController // This creates an empty controller without resource methods. // Correct way: php artisan make:controller PhotoController --resource
Quick Reference
| Command | Description |
|---|---|
| php artisan make:controller NameController --resource | Creates a resource controller with CRUD methods |
| Route::resource('name', NameController::class); | Registers resource routes for the controller |
| php artisan route:list | Lists all registered routes including resource routes |
Key Takeaways
Use
php artisan make:controller NameController --resource to create a resource controller with all CRUD methods.Register the controller in routes using
Route::resource() to enable URL endpoints.Resource controllers follow Laravel conventions for common actions like index, create, store, show, edit, update, and destroy.
Omitting the
--resource flag creates an empty controller without predefined methods.Use
php artisan route:list to verify your resource routes are registered correctly.