How to Create APIs with Laravel 12 Resource Controllers
Laravel 12: Tutorials, Guides & Best Practices

How to Create APIs with Laravel 12 Resource Controllers

Laravel 12 Resource Controllers for Category APIs

Author
Richard Mendes
March 21, 2025 • 8 mins

Create APIs with Laravel 12 Resource Controllers

Yo, what’s good? If you’re tryna level up your Laravel game and build some fire APIs, you’re in the right spot. Today, we’re messing with Laravel 12 and its Resource Controllers, specifically, a CategoryController to handle categories like a boss. Think CRUD ops with JSON responses, all wrapped in that sweet Laravel magic. Let’s break it down, step by step, no cap.

Kick It Off with a Controller

First things first, we’re hitting the terminal like it’s our BFF. Run this bad boy:

php artisan make:controller CategoryController --api

Boom! Laravel’s Artisan just dropped a fresh CategoryController.php file in app/Http/Controllers. The --api flag? That’s the MVP move, it sets up five RESTful methods (index, store, show, update, destroy) without the extra fluff. No view nonsense, just pure API vibes. Check your folder, it’s sitting there, ready to flex.

Hook Up the Routes

Next up, we’re sliding into routes/api.php to wire this thing up. Pop in this code:Next up, we’re sliding into routes/api.php to wire this thing up. Pop in this code:

<?php

use App\Http\Controllers\CategoryController;
use Illuminate\Support\Facades\Route;

Route::apiResource('categories', CategoryController::class);

Real talk, this Route::apiResource line is straight fire. It links your CategoryController to the /api/categories endpoint and auto-maps those five methods to RESTful routes. You’re getting GET /categories, POST /categories, and all the rest with one line. Laravel’s out here doing the heavy lifting, respect.

Set Up the Controller

Now, let’s juice up that CategoryController with some real action.

<?php

namespace App\Http\Controllers;

use App\Models\Category;
use Illuminate\Http\Request;

class CategoryController extends Controller
{
public function index()
{
$categories = Category::all();
if ($categories->isEmpty()) {
return response()->json(['message' => 'No categories found, fam'], 404);
}
return response()->json(['categories' => $categories], 200);
}

public function store(Request $request)
{
$category = new Category();
$category->name = $request->name;
$category->save();
return response()->json(['message' => 'Category dropped like it’s hot!', 'category' => $category], 200);
}

public function show(string $id)
{
$category = Category::find($id);
if (!$category) {
return response()->json(['message' => 'Category ghosted us, yo'], 404);
}
return response()->json($category, 200);
}

public function update(Request $request, string $id)
{
$category = Category::find($id);
if (!$category) {
return response()->json(['message' => 'Category MIA, fam'], 404);
}
$category->name = $request->name;
$category->save();
return response()->json(['message' => 'Category leveled up!', 'category' => $category], 200);
}

public function destroy(string $id)
{
$category = Category::find($id);
if (!$category) {
return response()->json(['message' => 'Category nowhere to be found'], 404);
}
$category->delete();
return response()->json(['message' => 'Category yeeted successfully'], 200);
}
}

Check the vibe:

  1. Index: Grabs all categories or throws a 404 if it’s empty, keepin’ it real.
  2. Store: Slaps a new category in the DB and flexes it back with a 200.
  3. Show: Finds one by ID or dips with a 404 if it’s a no-show.
  4. Update: Updates the name if it exists, smooth as butter.
  5. Destroy: Yeets a category and confirms it with swagger.
  6. This controller’s spitting JSON responses like a pro. You’re golden!

Test It with Postman

Time to see this baby shine. Fire up Postman (or whatever you rock with) and hit these endpoints. Assuming you’re running local, here’s the lineup:

API Endpoints for Testing

  1. GET http://localhost/api/categories
  2. POST http://localhost/api/categories
  3. GET http://localhost/api/categories/1
  4. PUT http://localhost/api/categories/1
  5. DELETE http://localhost/api/categories/1


Latest Articles