Laravel 12 Controllers - Beginner Tutorial
Controllers in Laravel are like managers that handle requests from the user and send responses back. They sit between routes and models keeping your code clean and organized.
In this tutorial, you will learn everything about controllers in Laravel 12, including how to create them, different types of controllers, how to call models and APIs from controllers, handle data, use resource controllers and much more.
Let’s begin.
What is a Controller in Laravel 12?
A controller is a class stored in app/Http/Controllers folder.
It helps you group related request handling logic into a single place.
For example, if you are building a blog, you can create a PostController to handle all requests related to posts — creating, updating, deleting, and displaying posts.
Example:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class PostController extends Controller {
public function index() {
return view('posts.index');
}
}
This method can then be linked to a route like this:
This is the basic idea of how routes call controller methods.
How to Create a Controller in Laravel 12
Laravel gives you an Artisan command to quickly generate controllers.
Basic command:
This will create PostController.php inside app/Http/Controllers.
Create controller in a subfolder:
Create controller with model and resource:
This command saves a lot of time because it also prepares all standard CRUD methods automatically.
Types of Controllers in Laravel
Laravel has different types of controllers based on their purpose. Let’s quickly explore them.
(i)- Normal Controller
This is a standard controller that can have multiple methods like index, create, store, edit, update, and destroy.
Use this when you want to manage many actions inside one controller.
(ii)- Invokable Controller
An invokable controller has only one method called __invoke().
It’s useful when you just need one action in that controller.
Create command:
Example:
Route:
(iii)- Resource Controller
A resource controller is built for CRUD operations.
It automatically includes 7 methods:
- index
- create
- store
- show
- edit
- update
- destroy
Create command:
Resource route:
Laravel Resource Controller Example (With Model)
Step 1 — Create Model with Migration and Resource Controller
This creates:
-
Postmodel -
create_posts_tablemigration -
PostControllerwith resource methods
Step 2 — Define Routes
Step 3 — Example Store Method
This is a clean way to build CRUD in Laravel 12.
Handling Data in Laravel 12 Controllers
Controllers are the main place where you handle input data from forms or APIs and then send data back to the views.
| Handle Request Data | $title = $request->input('title'); |
| Handle Date Fields | use Carbon\Carbon; $date = Carbon::now(); |
| Get URL Parameters |
public function show($id) { return "Post ID is " . $id; } |
| Get ENV Variable | $apiKey = env('API_KEY'); |
| Get Validation Errors | $request->validate([ 'title' => 'required' ]); |
This shows how flexible Laravel controllers are for handling different kinds of data.
Calling Models and APIs from Controllers
You can directly call your models inside controllers to interact with the database.
Example:
You can also call APIs from controllers using Laravel’s Http client:
Route to Controller in Laravel 12
You always need to connect your controller methods with routes.
Example Route:
This route will call the index method from the PostController.
Access Control in Laravel Controllers
Controllers can use middleware to restrict access. This is part of Laravel access control.
Example:
You can also use authorization policies inside controllers:
Controller Best Practices in Laravel 12
Here are a few good habits:
-
Use resource controllers for CRUD
-
Keep logic small — don’t write long functions
-
Use Request classes for validation
-
Organize controllers in subfolders if your app grows
-
Use route model binding instead of manually finding records
These small steps make your controller code clean and maintainable.
Useful Artisan Commands for Controllers
| Task | Command |
|---|---|
| Create controller | php artisan make:controller NameController |
| Create controller in subfolder | php artisan make:controller Admin/NameController |
| Create invokable controller | php artisan make:controller NameController --invokable |
| Create resource controller | php artisan make:controller NameController --resource |
| Create controller with model | php artisan make:controller NameController --model=Model |
| Create model + migration + controller | php artisan make:model Post -mcr |
| Delete controller | delete file manually from app/Http/Controllers |
FAQs about Laravel 12 Controllers
Q: What is a controller in Laravel 12?
A controller is a PHP class that handles user requests and sends responses.
Q: How to make a controller in Laravel 12?
Use the command php artisan make:controller YourController.
Q: What is a resource controller in Laravel 12?
It is a controller that comes with 7 built-in CRUD methods.
Q: How to create a controller in a specific folder?
Use php artisan make:controller Admin/YourController.
Q: What is an invokable controller?
It is a controller with only one method __invoke().
Q: How to call model functions in controller?
Just use ModelName::all() or other Eloquent methods.
Q: How to get URL parameters in controller?
Use method arguments like public function show($id).
Q: How to get ENV variable in controller?
Use env('KEY_NAME').
Q: How to handle dates in controller?
Use the Carbon class to work with dates.
Q: How to create controller with model and migration?
Use php artisan make:model Post -mcr.
Final Thoughts
Controllers are one of the most important building blocks in Laravel 12.
They keep your code organized, reusable and easier to test.
Once you understand how to:
-
create controllers
-
handle data
-
use resource controllers
-
and connect routes with controllers
you can build clean and scalable Laravel projects easily.