Laravel 8 Controllers Explained with Examples 🚀
A controller in Laravel is used to handle application logic and organize code better by separating business logic from routes. Controllers allow you to group related request handling logic into a single class.
1. Creating a Controller
To create a controller, run:
php artisan make:controller UserController
✅ This generates a new controller in:
app/Http/Controllers/UserController.php
2. Basic Controller Example
Step 1: Define a Route (routes/web.php)
use App\Http\Controllers\UserController;
Route::get('/user', [UserController::class, 'index']);
Step 2: Define Controller Method (app/Http/Controllers/UserController.php)
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class UserController extends Controller
{
public function index()
{
return "Welcome to the User Dashboard!";
}
}
✅ Visiting /user
in a browser will display:
"Welcome to the User Dashboard!"
3. Passing Data to Views
Instead of returning a string, controllers often return a view.
Step 1: Define a Route
Route::get('/profile', [UserController::class, 'profile']);
Step 2: Create a View (resources/views/profile.blade.php
)
<!DOCTYPE html>
<html>
<head>
<title>User Profile</title>
</head>
<body>
<h1>Welcome, {{ $name }}!</h1>
</body>
</html>
Step 3: Modify Controller Method
public function profile()
{
$name = "John Doe";
return view('profile', compact('name'));
}
✅ Visiting /profile
will render the profile.blade.php
view with "Welcome, John Doe!".
4. Controller with Route Parameters
Step 1: Define a Route with a Parameter
Route::get('/user/{id}', [UserController::class, 'show']);
Step 2: Modify Controller Method
public function show($id)
{
return "User ID: " . $id;
}
✅ Visiting /user/5
will output "User ID: 5".
5. Controller with Route Model Binding
Instead of manually fetching a model, Laravel automatically injects it.
Step 1: Define a Route
use App\Models\User;
Route::get('/user/{user}', [UserController::class, 'show']);
Step 2: Modify Controller Method
public function show(User $user)
{
return $user; // Automatically fetches user from the database
}
✅ Laravel will automatically find the user by ID and return its details.
6. Resource Controller (CRUD)
A resource controller handles CRUD operations efficiently.
Step 1: Create a Resource Controller
php artisan make:controller ProductController --resource
✅ This generates:
app/Http/Controllers/ProductController.php
Step 2: Define a Resource Route
use App\Http\Controllers\ProductController;
Route::resource('products', ProductController::class);
This automatically creates routes for:
| HTTP Verb | URI | Action | Method in Controller |
|-----------|---------------|--------------|---------------------|
| GET | /products | View all | index()
|
| GET | /products/create | Show form | create()
|
| POST | /products | Store data | store()
|
| GET | /products/{id} | Show single | show()
|
| GET | /products/{id}/edit | Edit form | edit()
|
| PUT/PATCH | /products/{id} | Update data | update()
|
| DELETE | /products/{id} | Delete data | destroy()
|
7. Example CRUD Methods in ProductController
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Product;
class ProductController extends Controller
{
// Show all products
public function index()
{
$products = Product::all();
return view('products.index', compact('products'));
}
// Show form to create a product
public function create()
{
return view('products.create');
}
// Store new product in database
public function store(Request $request)
{
Product::create($request->all());
return redirect()->route('products.index')->with('success', 'Product created successfully!');
}
// Show single product
public function show(Product $product)
{
return view('products.show', compact('product'));
}
// Show form to edit product
public function edit(Product $product)
{
return view('products.edit', compact('product'));
}
// Update product details
public function update(Request $request, Product $product)
{
$product->update($request->all());
return redirect()->route('products.index')->with('success', 'Product updated successfully!');
}
// Delete product
public function destroy(Product $product)
{
$product->delete();
return redirect()->route('products.index')->with('success', 'Product deleted successfully!');
}
}
8. Middleware in Controller
To restrict access, you can apply middleware inside a controller.
Applying Middleware to Specific Methods
public function __construct()
{
$this->middleware('auth')->only(['create', 'edit', 'destroy']);
}
✅ Only authenticated users can access create, edit, and destroy methods.
Conclusion
Laravel controllers make your application clean, scalable, and organized. They allow better separation of routes, views, and business logic. 🎯💡
At Online Learner, we're on a mission to ignite a passion for learning and empower individuals to reach their full potential. Founded by a team of dedicated educators and industry experts, our platform is designed to provide accessible and engaging educational resources for learners of all ages and backgrounds.
Copyright 2023-2025 © All rights reserved.