Creating APIs in Laravel 12 with Resource Controllers
Laravel 12: Tutorials, Guides & Best Practices

Creating APIs in Laravel 12 with Resource Controllers

Mastering APIs in Laravel 12 with Resource Controllers

Author
Richard Mendes
March 21, 2025 • 10 mins

Laravel 12 makes API development a breeze with its powerful Resource Controllers. If you're building a RESTful API for a small project or a complex application, mastering API Resource Controllers can save you time and effort. In this guide, I will walk through creating a fully functional API in Laravel 12, starting with a simple command to generate a controller. Let’s dive in and see how easy it is to get started with the CountryController as our example!

Generate the API Resource ControllerGenerate the API Resource Controller

To kick things off, open your terminal in your Laravel 12 project directory and run the following Artisan command:

php artisan make:controller CountryController --api

This creates a new file, CountryController.php, in the app/Http/Controllers directory. The --api flag automatically sets up a resourceful controller with five RESTful methods, That are, index, store, show, update, and destroy—perfect for handling API requests without the clutter of unnecessary view-related methods.

Define the API Route in api.phpDefine the API Route in api.php

Now that we’ve created the CountryController, let’s connect it to a route so our API can handle requests. Laravel 12 makes this simple with the api.php routes file, designed specifically for API endpoints. Open routes/api.php and add the following code:

<?php

use App\Http\Controllers\CountryController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;

Route::get('/user', function (Request $request) {
return $request->user();
})->middleware('auth:sanctum');

Route::apiResource('countries', CountryController::class);

Here’s what’s happening:

  1. We import the CountryController with use App\Http\Controllers\CountryController;.
  2. The Route::apiResource('countries', CountryController::class); line registers RESTful routes for our CountryController. This single line creates five routes (GET /countries, POST /countries, GET /countries/{id}, PUT/PATCH /countries/{id}, and DELETE /countries/{id}) to handle all standard API operations.
  3. The countries part becomes the base URL for your API (e.g., /api/countries), and Laravel automatically prefixes it with /api/ since this is in api.php.

Save the file, and your API is now wired up to the controller.

Implement the API Resource Controller with Data and JSON Responses

<?php

namespace App\Http\Controllers;

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

class CountryController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index()
{
$countries = Country::all();

if ($countries->isEmpty()) {
return response()->json(['message' => 'No countries found'], 404);
}

return response()->json(['countries' => $countries], 200);
}

/**
* Store a newly created resource in storage.
*/
public function store(Request $request)
{
$country = new Country();
$country->name = $request->name;
$country->code = $request->code;
$country->save();

return response()->json(['message' => 'Country created successfully', 'country' => $country], 201);
}

/**
* Display the specified resource.
*/
public function show(string $id)
{
$country = Country::find($id);

if (!$country) {
return response()->json(['message' => 'Country not found'], 404);
}

return response()->json($country, 200);
}

/**
* Update the specified resource in storage.
*/
public function update(Request $request, string $id)
{
$country = Country::find($id);

if (!$country) {
return response()->json(['message' => 'Country not found'], 404);
}

$country->name = $request->name;
$country->code = $request->code;
$country->save();

return response()->json(['message' => 'Country updated successfully', 'country' => $country], 200);
}

/**
* Remove the specified resource from storage.
*/
public function destroy(string $id)
{
$country = Country::find($id);

if (!$country) {
return response()->json(['message' => 'Country not found'], 404);
}

$country->delete();

return response()->json(['message' => 'Country deleted successfully'], 200);
}
}

API Endpoints for Testing in Postman

GET http://localhost/api/countries
POST http://localhost/api/countries
GET http://localhost/api/countries/1
PUT http://localhost/api/countries/1
DELETE http://localhost/api/countries/1


Latest Articles