Tutorial REST API Laravel 12 untuk Pemula

Tutorial REST API Laravel 12 untuk Pemula

author

Teguh Bagas M

Web Developer

Tags

Share this post

Tutorial REST API Laravel 12 untuk Pemula

Apa itu API, REST, dan RESTful?

🔍 Pengertian API (Application Programming Interface)

API adalah jembatan yang memungkinkan dua aplikasi berbeda untuk saling berkomunikasi. Bayangkan seperti pelayan di restoran - Anda (aplikasi frontend) memesan makanan kepada pelayan (API), dan pelayan menyampaikan pesanan ke dapur (database/server) lalu membawa makanan kembali ke Anda.

Contoh sederhana:

  • Aplikasi mobile Instagram menggunakan API untuk mengambil foto dari server
  • Website e-commerce menggunakan API untuk menampilkan produk dari database
  • Aplikasi cuaca menggunakan API untuk mendapatkan data cuaca terkini

📱 Jenis-Jenis API Berdasarkan Akses

1. Public API (Open API)

  • Definisi: API yang dapat diakses oleh siapa saja tanpa batasan khusus
  • Contoh:
    • API cuaca OpenWeatherMap
    • API quotes gratis
    • API data negara RestCountries
  • Karakteristik: Gratis, dokumentasi lengkap, rate limit rendah
  • Analogi: Seperti perpustakaan umum yang bisa diakses siapa saja

2. Private API (Internal API)

  • Definisi: API yang hanya digunakan dalam satu organisasi/perusahaan
  • Contoh:
    • API untuk komunikasi antar microservices dalam perusahaan
    • API internal untuk aplikasi mobile dan web perusahaan
  • Karakteristik: Akses terbatas, keamanan tinggi, performa optimal
  • Analogi: Seperti sistem interkom dalam kantor

3. Partner API

  • Definisi: API yang dibagikan kepada mitra bisnis atau developer tertentu
  • Contoh:
    • API payment gateway (Midtrans, Stripe)
    • API shipping (JNE, Shopee Express)
  • Karakteristik: Akses dengan kontrak/agreement, dokumentasi khusus
  • Analogi: Seperti akses khusus ke ruangan VIP

4. Composite API

  • Definisi: API yang menggabungkan beberapa API dalam satu request
  • Contoh:
    • Dashboard yang mengambil data user + posts + analytics sekaligus
    • E-commerce yang mengambil produk + inventory + harga sekaligus
  • Karakteristik: Efisien, mengurangi jumlah request
  • Analogi: Seperti paket combo meal di restoran

🌐 Apa itu REST (Representational State Transfer)?

REST adalah sebuah gaya arsitektur untuk membangun web services. REST menggunakan protokol HTTP yang sudah familiar dan memiliki prinsip-prinsip sederhana:

  1. Stateless - Setiap request berdiri sendiri, server tidak menyimpan informasi tentang client
  2. Client-Server - Pemisahan antara frontend dan backend
  3. Cacheable - Response dapat di-cache untuk performa yang lebih baik
  4. Uniform Interface - Menggunakan HTTP methods yang standar

📋 HTTP Methods dalam REST

  • GET - Mengambil data (seperti membaca buku)
  • POST - Membuat data baru (seperti menulis halaman baru)
  • PUT - Update seluruh data (seperti mengganti seluruh halaman)
  • PATCH - Update sebagian data (seperti mengedit paragraf)
  • DELETE - Menghapus data (seperti merobek halaman)

✅ RESTful API

RESTful API adalah API yang mengikuti prinsip-prinsip REST dengan benar. Contoh endpoint RESTful:

  • GET /api/posts - Ambil semua artikel
  • GET /api/posts/1 - Ambil artikel dengan ID 1
  • POST /api/posts - Buat artikel baru
  • PUT /api/posts/1 - Update artikel dengan ID 1
  • DELETE /api/posts/1 - Hapus artikel dengan ID 1

🚨 Hal Wajib yang Harus Diketahui Developer API

1. Format Data - JSON (JavaScript Object Notation)

JSON adalah format standar untuk pertukaran data di API modern. Format ini mudah dibaca manusia dan mudah diproses komputer.

Struktur JSON Dasar:

{ "nama_field": "nilai", "angka": 123, "boolean": true, "array": [1, 2, 3], "objek": { "nested_field": "nilai nested" }, "null_value": null }

Contoh Response API yang Baik:

{ "success": true, "message": "Data berhasil diambil", "data": { "id": 1, "title": "Belajar API Laravel", "author": "John Doe", "is_published": true, "created_at": "2025-08-08T10:00:00Z" } }

Aturan JSON yang Perlu Diingat:

  • String harus dalam tanda kutip ganda ""
  • Key/Field harus dalam tanda kutip ganda
  • Boolean: true atau false (lowercase)
  • Number: 123, 45.67 (tanpa tanda kutip)
  • Array: [1, 2, 3] atau ["a", "b", "c"]
  • Object: {"key": "value"}
  • Null: null (bukan "null")

2. HTTP Status Codes - Kode yang Sering Muncul

2xx - Success (Berhasil)

  • 200 OK: Request berhasil, data dikembalikan
    • Contoh: GET artikel berhasil
  • 201 Created: Data baru berhasil dibuat
    • Contoh: POST artikel baru berhasil
  • 204 No Content: Request berhasil, tapi tidak ada data yang dikembalikan
    • Contoh: DELETE berhasil

⚠️ 4xx - Client Error (Kesalahan dari Client)

  • 400 Bad Request: Format request salah
    • Contoh: JSON tidak valid, parameter wajib hilang
  • 401 Unauthorized: Belum login/authentication gagal
    • Contoh: Akses endpoint yang butuh login tanpa token
  • 403 Forbidden: Sudah login tapi tidak punya akses
    • Contoh: User biasa coba akses admin panel
  • 404 Not Found: Resource tidak ditemukan
    • Contoh: GET /api/posts/999 (ID 999 tidak ada)
  • 405 Method Not Allowed: HTTP method salah
    • Contoh: POST ke endpoint yang cuma terima GET
  • 422 Unprocessable Entity: Validation error
    • Contoh: Email format salah, password terlalu pendek
  • 429 Too Many Requests: Rate limit exceeded
    • Contoh: Terlalu banyak request dalam waktu singkat

🔥 5xx - Server Error (Kesalahan dari Server)

  • 500 Internal Server Error: Error di server/kode program
    • Contoh: Database connection gagal, bug di code
  • 502 Bad Gateway: Server gateway error
  • 503 Service Unavailable: Server maintenance/overload

3. Headers yang Penting

Content-Type: application/json # Format data yang dikirim Accept: application/json # Format data yang diharapkan Authorization: Bearer token123 # Token untuk authentication

4. Struktur Response yang Konsisten

Response Sukses:

{ "success": true, "message": "Operasi berhasil", "data": {...} }

Response Error:

{ "success": false, "message": "Terjadi kesalahan", "errors": { "email": ["Format email tidak valid"], "password": ["Password minimal 8 karakter"] } }

5. Best Practices Naming Convention

  • URL: gunakan kebab-case /api/blog-posts
  • JSON Field: gunakan snake_case created_at, is_published
  • Endpoint: gunakan noun (kata benda), bukan verb
    • ✅ Good: GET /api/users
    • ❌ Bad: GET /api/get-users

6. Security Basics

  • Jangan expose sensitive data (password, secret key)
  • Gunakan HTTPS di production
  • Validasi semua input dari user
  • Rate limiting untuk mencegah spam request

Tujuan Pembelajaran

Setelah mengikuti tutorial ini, Anda akan mampu:

  1. Memahami konsep dasar REST API
  2. Membuat API sederhana dengan Laravel 12
  3. Mengimplementasikan CRUD (Create, Read, Update, Delete)
  4. Testing API menggunakan tools seperti Postman
  5. Memahami struktur response JSON yang baik

Prerequisites

  • PHP >= 8.2
  • Composer (untuk install Laravel)
  • Database MySQL atau SQLite
  • Postman atau Thunder Client (untuk testing)
  • Text Editor (VS Code recommended)

Langkah 1: Setup Project Laravel

1.1 Buat Project Laravel Baru

composer create-project laravel/laravel blog-api
cd blog-api

1.2 Konfigurasi Database

Edit file .env (file konfigurasi Laravel):

APP_NAME="Blog API" APP_URL=http://localhost:8000 DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=blog_api DB_USERNAME=root DB_PASSWORD=

💡 Tips: Jika tidak ada MySQL, Anda bisa gunakan SQLite dengan mengubah DB_CONNECTION=sqlite dan hapus baris DB_HOST, DB_PORT, dll.

1.3 Test Instalasi

php artisan serve

Buka http://localhost:8000 di browser, jika muncul halaman Laravel berarti instalasi berhasil!


Langkah 2: Membuat Database dan Model

2.1 Buat Model Post dengan Migration

php artisan make:model Post -m

Perintah ini akan membuat 2 file:

  • app/Models/Post.php (Model untuk berinteraksi dengan database)
  • database/migrations/xxxx_create_posts_table.php (Blueprint database)

2.2 Setup Migration (Blueprint Database)

Edit file migration di database/migrations/xxxx_create_posts_table.php:

<?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; return new class extends Migration { public function up(): void { Schema::create('posts', function (Blueprint $table) { $table->id(); // Primary key auto increment $table->string('title'); // Judul artikel $table->text('content'); // Isi artikel $table->string('author'); // Penulis artikel $table->boolean('is_published')->default(false); // Status publish $table->timestamps(); // created_at dan updated_at }); } public function down(): void { Schema::dropIfExists('posts'); } };

2.3 Setup Model

Edit file app/Models/Post.php:

<?php namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; class Post extends Model { use HasFactory; // Field yang boleh diisi secara mass assignment protected $fillable = [ 'title', 'content', 'author', 'is_published' ]; // Cast tipe data protected $casts = [ 'is_published' => 'boolean', 'created_at' => 'datetime', 'updated_at' => 'datetime', ]; }

2.4 Jalankan Migration

php artisan migrate

Perintah ini akan membuat tabel posts di database Anda.


Langkah 3: Membuat API Controller

3.1 Buat Controller

php artisan make:controller PostController

3.2 Edit Controller

Edit file app/Http/Controllers/PostController.php:

<?php namespace App\Http\Controllers; use App\Models\Post; use Illuminate\Http\Request; use Illuminate\Http\JsonResponse; class PostController extends Controller { // GET /api/posts - Ambil semua artikel public function index(): JsonResponse { $posts = Post::all(); return response()->json([ 'success' => true, 'message' => 'Data artikel berhasil diambil', 'data' => $posts ]); } // GET /api/posts/{id} - Ambil satu artikel public function show($id): JsonResponse { $post = Post::find($id); if (!$post) { return response()->json([ 'success' => false, 'message' => 'Artikel tidak ditemukan' ], 404); } return response()->json([ 'success' => true, 'message' => 'Detail artikel berhasil diambil', 'data' => $post ]); } // POST /api/posts - Buat artikel baru public function store(Request $request): JsonResponse { // Validasi input $request->validate([ 'title' => 'required|string|max:255', 'content' => 'required|string', 'author' => 'required|string|max:255', 'is_published' => 'boolean' ]); // Buat artikel baru $post = Post::create([ 'title' => $request->title, 'content' => $request->content, 'author' => $request->author, 'is_published' => $request->is_published ?? false ]); return response()->json([ 'success' => true, 'message' => 'Artikel berhasil dibuat', 'data' => $post ], 201); } // PUT /api/posts/{id} - Update artikel public function update(Request $request, $id): JsonResponse { $post = Post::find($id); if (!$post) { return response()->json([ 'success' => false, 'message' => 'Artikel tidak ditemukan' ], 404); } // Validasi input $request->validate([ 'title' => 'required|string|max:255', 'content' => 'required|string', 'author' => 'required|string|max:255', 'is_published' => 'boolean' ]); // Update artikel $post->update([ 'title' => $request->title, 'content' => $request->content, 'author' => $request->author, 'is_published' => $request->is_published ?? $post->is_published ]); return response()->json([ 'success' => true, 'message' => 'Artikel berhasil diperbarui', 'data' => $post ]); } // DELETE /api/posts/{id} - Hapus artikel public function destroy($id): JsonResponse { $post = Post::find($id); if (!$post) { return response()->json([ 'success' => false, 'message' => 'Artikel tidak ditemukan' ], 404); } $post->delete(); return response()->json([ 'success' => true, 'message' => 'Artikel berhasil dihapus' ]); } }

Langkah 4: Setup Routes API

Install API

php artisan install:api

Edit file routes/api.php:

<?php use App\Http\Controllers\PostController; use Illuminate\Support\Facades\Route; // Info API Route::get('/', function () { return response()->json([ 'message' => 'Selamat datang di Blog API', 'version' => '1.0', 'endpoints' => [ 'GET /api/posts' => 'Ambil semua artikel', 'GET /api/posts/{id}' => 'Ambil artikel tertentu', 'POST /api/posts' => 'Buat artikel baru', 'PUT /api/posts/{id}' => 'Update artikel', 'DELETE /api/posts/{id}' => 'Hapus artikel' ] ]); }); // CRUD Routes untuk Posts Route::get('/posts', [PostController::class, 'index']); Route::get('/posts/{id}', [PostController::class, 'show']); Route::post('/posts', [PostController::class, 'store']); Route::put('/posts/{id}', [PostController::class, 'update']); Route::delete('/posts/{id}', [PostController::class, 'destroy']);

Langkah 5: Menambah Data Sample

5.1 Buat Factory untuk Dummy Data

php artisan make:factory PostFactory

Edit database/factories/PostFactory.php:

<?php namespace Database\Factories; use Illuminate\Database\Eloquent\Factories\Factory; class PostFactory extends Factory { public function definition(): array { return [ 'title' => fake()->sentence(6, true), 'content' => fake()->paragraphs(3, true), 'author' => fake()->name(), 'is_published' => fake()->boolean(70), // 70% kemungkinan true ]; } }

5.2 Buat Seeder

php artisan make:seeder PostSeeder

Edit database/seeders/PostSeeder.php:

<?php namespace Database\Seeders; use App\Models\Post; use Illuminate\Database\Seeder; class PostSeeder extends Seeder { public function run(): void { // Buat 10 artikel sample Post::factory(10)->create(); // Buat artikel manual untuk testing Post::create([ 'title' => 'Artikel Pertama Saya', 'content' => 'Ini adalah artikel pertama yang saya buat menggunakan API Laravel', 'author' => 'John Doe', 'is_published' => true ]); } }

5.3 Update DatabaseSeeder

Edit database/seeders/DatabaseSeeder.php:

<?php namespace Database\Seeders; use Illuminate\Database\Seeder; class DatabaseSeeder extends Seeder { public function run(): void { $this->call([ PostSeeder::class, ]); } }

5.4 Jalankan Seeder

php artisan db:seed

Langkah 6: Testing API

6.1 Jalankan Server

php artisan serve

6.2 Test Endpoints dengan Postman/Thunder Client

📋 1. GET - Ambil Semua Artikel

  • Method: GET
  • URL: http://localhost:8000/api/posts
  • Response:
{ "success": true, "message": "Data artikel berhasil diambil", "data": [ { "id": 1, "title": "Artikel Pertama Saya", "content": "Ini adalah artikel pertama...", "author": "John Doe", "is_published": true, "created_at": "2025-08-08T10:00:00.000000Z", "updated_at": "2025-08-08T10:00:00.000000Z" } ] }

📋 2. GET - Ambil Artikel Tertentu

  • Method: GET
  • URL: http://localhost:8000/api/posts/1

📋 3. POST - Buat Artikel Baru

  • Method: POST
  • URL: http://localhost:8000/api/posts
  • Headers: Content-Type: application/json
  • Body (JSON):
{ "title": "Tutorial Laravel API", "content": "Ini adalah tutorial untuk membuat API dengan Laravel", "author": "Jane Smith", "is_published": true }

📋 4. PUT - Update Artikel

  • Method: PUT
  • URL: http://localhost:8000/api/posts/1
  • Headers: Content-Type: application/json
  • Body (JSON):
{ "title": "Tutorial Laravel API - Updated", "content": "Ini adalah tutorial yang sudah diperbarui", "author": "Jane Smith", "is_published": true }

📋 5. DELETE - Hapus Artikel

  • Method: DELETE
  • URL: http://localhost:8000/api/posts/1

Langkah 7: Menambah Fitur Pencarian

Edit method index di PostController.php:

public function index(Request $request): JsonResponse { $query = Post::query(); // Pencarian berdasarkan title if ($request->has('search')) { $search = $request->search; $query->where('title', 'like', "%{$search}%") ->orWhere('content', 'like', "%{$search}%"); } // Filter berdasarkan status publish if ($request->has('published')) { $published = $request->boolean('published'); $query->where('is_published', $published); } // Filter berdasarkan author if ($request->has('author')) { $query->where('author', 'like', "%{$request->author}%"); } // Urutkan berdasarkan yang terbaru $posts = $query->orderBy('created_at', 'desc')->get(); return response()->json([ 'success' => true, 'message' => 'Data artikel berhasil diambil', 'data' => $posts, 'total' => $posts->count() ]); }

7.2 Test Pencarian

  • Search: http://localhost:8000/api/posts?search=laravel
  • Filter Published: http://localhost:8000/api/posts?published=true
  • Filter Author: http://localhost:8000/api/posts?author=john
  • Kombinasi: http://localhost:8000/api/posts?search=tutorial&published=true

Langkah 8: Menangani Error dengan Baik

8.1 Custom Error Handling

Buat file app/Http/Controllers/BaseController.php:

<?php namespace App\Http\Controllers; use Illuminate\Http\JsonResponse; class BaseController extends Controller { /** * Success response */ public function sendResponse($data, $message = 'Success', $code = 200): JsonResponse { return response()->json([ 'success' => true, 'message' => $message, 'data' => $data ], $code); } /** * Error response */ public function sendError($message, $errors = [], $code = 400): JsonResponse { $response = [ 'success' => false, 'message' => $message ]; if (!empty($errors)) { $response['errors'] = $errors; } return response()->json($response, $code); } }

8.2 Update PostController

Ubah PostController untuk extend BaseController:

<?php namespace App\Http\Controllers; use App\Models\Post; use Illuminate\Http\Request; use Illuminate\Http\JsonResponse; use Illuminate\Validation\ValidationException; class PostController extends BaseController // Extend BaseController { public function index(Request $request): JsonResponse { try { $query = Post::query(); // Logic pencarian (sama seperti sebelumnya) if ($request->has('search')) { $search = $request->search; $query->where('title', 'like', "%{$search}%") ->orWhere('content', 'like', "%{$search}%"); } if ($request->has('published')) { $published = $request->boolean('published'); $query->where('is_published', $published); } $posts = $query->orderBy('created_at', 'desc')->get(); return $this->sendResponse($posts, 'Data artikel berhasil diambil'); } catch (\Exception $e) { return $this->sendError('Terjadi kesalahan saat mengambil data', [], 500); } } public function show($id): JsonResponse { try { $post = Post::find($id); if (!$post) { return $this->sendError('Artikel tidak ditemukan', [], 404); } return $this->sendResponse($post, 'Detail artikel berhasil diambil'); } catch (\Exception $e) { return $this->sendError('Terjadi kesalahan saat mengambil detail artikel', [], 500); } } public function store(Request $request): JsonResponse { try { $request->validate([ 'title' => 'required|string|max:255', 'content' => 'required|string', 'author' => 'required|string|max:255', 'is_published' => 'boolean' ]); $post = Post::create([ 'title' => $request->title, 'content' => $request->content, 'author' => $request->author, 'is_published' => $request->is_published ?? false ]); return $this->sendResponse($post, 'Artikel berhasil dibuat', 201); } catch (ValidationException $e) { return $this->sendError('Data tidak valid', $e->errors(), 422); } catch (\Exception $e) { return $this->sendError('Terjadi kesalahan saat membuat artikel', [], 500); } } public function update(Request $request, $id): JsonResponse { try { $post = Post::find($id); if (!$post) { return $this->sendError('Artikel tidak ditemukan', [], 404); } // Validasi input $request->validate([ 'title' => 'required|string|max:255', 'content' => 'required|string', 'author' => 'required|string|max:255', 'is_published' => 'boolean' ]); // Update artikel $post->update([ 'title' => $request->title, 'content' => $request->content, 'author' => $request->author, 'is_published' => $request->has('is_published') ? $request->is_published : $post->is_published ]); return $this->sendResponse($post, 'Artikel berhasil diperbarui', 200); } catch (ValidationException $e) { return $this->sendError('Data tidak valid', $e->errors(), 422); } catch (\Exception $e) { return $this->sendError('Terjadi kesalahan saat memperbarui artikel', [], 500); } } // Update method destroy juga public function destroy($id): JsonResponse { try { $post = Post::find($id); if (!$post) { return $this->sendError('Artikel tidak ditemukan', [], 404); } $post->delete(); return $this->sendResponse([], 'Artikel berhasil dihapus'); } catch (\Exception $e) { return $this->sendError('Terjadi kesalahan saat menghapus artikel', [], 500); } } }

Langkah 9: Menambah Pagination

9.1 Update Method Index dengan Pagination

public function index(Request $request): JsonResponse { try { $query = Post::query(); // Logic pencarian (sama seperti sebelumnya) if ($request->has('search')) { $search = $request->search; $query->where('title', 'like', "%{$search}%") ->orWhere('content', 'like', "%{$search}%"); } if ($request->has('published')) { $published = $request->boolean('published'); $query->where('is_published', $published); } // Pagination - default 10 per page $perPage = $request->get('per_page', 10); $posts = $query->orderBy('created_at', 'desc')->paginate($perPage); return response()->json([ 'success' => true, 'message' => 'Data artikel berhasil diambil', 'data' => $posts->items(), // Data artikel 'pagination' => [ 'current_page' => $posts->currentPage(), 'per_page' => $posts->perPage(), 'total' => $posts->total(), 'last_page' => $posts->lastPage(), 'from' => $posts->firstItem(), 'to' => $posts->lastItem() ] ]); } catch (\Exception $e) { return $this->sendError('Terjadi kesalahan saat mengambil data', [], 500); } }

9.2 Test Pagination

  • Page 1: http://localhost:8000/api/posts?page=1
  • Page 2: http://localhost:8000/api/posts?page=2
  • Custom per page: http://localhost:8000/api/posts?per_page=5

Langkah 10: Testing dan Debugging

### Get all blog posts GET http://localhost:8000/api/posts Accept: application/json ### Get all blog posts with pagination GET http://localhost:8000/api/posts?page=1 ### Get all blog posts with search query GET http://localhost:8000/api/posts?search=artikel Accept: application/json ### Get single blog post by ID GET http://localhost:8000/api/posts/12 Accept: application/json ### Create a new blog post POST http://localhost:8000/api/posts Content-Type: application/json Accept: application/json { "title": "Belajar Laravel API", "content": "Ini adalah konten dari post pertama saya.", "author": "Bagas", "is_published": true } ### Update blog post PUT http://localhost:8000/api/posts/12 Content-Type: application/json Accept: application/json { "title": "Belajar Laravel API (Update)", "content": "Konten ini sudah diperbarui.", "author": "Bagas", "is_published": false } ### Delete blog post DELETE http://localhost:8000/api/posts/12 Accept: application/json

10.1 Test Semua Endpoint

Gunakan Postman Collection berikut:

Collection Test Cases:

  1. GET All Posts - Status: 200
  2. GET Single Post - Status: 200
  3. GET Non-existent Post - Status: 404
  4. POST Valid Data - Status: 201
  5. POST Invalid Data - Status: 422
  6. PUT Update Post - Status: 200
  7. DELETE Post - Status: 200
  8. Search Posts - Status: 200
  9. Filter Published - Status: 200
  10. Pagination - Status: 200

Best Practices yang Sudah Diterapkan

✅ 1. Konsistensi Response Format

{ "success": true/false, "message": "Pesan yang jelas", "data": {...} // Hanya ada jika success: true }

✅ 2. HTTP Status Codes yang Tepat

  • 200: OK (GET, PUT berhasil)
  • 201: Created (POST berhasil)
  • 404: Not Found (Resource tidak ada)
  • 422: Unprocessable Entity (Validation error)
  • 500: Internal Server Error

✅ 3. Validation yang Proper

  • Required fields divalidasi
  • Max length untuk string
  • Type checking untuk boolean

✅ 4. Error Handling

  • Try-catch untuk semua method
  • Pesan error yang informatif
  • Tidak expose sensitive information

✅ 5. RESTful URL Structure

  • /posts untuk collection
  • /posts/{id} untuk specific resource
  • HTTP methods sesuai operasi

Kesimpulan

Selamat! 🎉 Anda telah berhasil membuat REST API sederhana tapi lengkap dengan Laravel 12.

Yang Sudah Kita Pelajari:

  • ✅ Pengertian API, REST, dan RESTful
  • ✅ Setup Laravel project dari awal
  • ✅ Membuat Model dan Migration
  • ✅ CRUD Operations lengkap
  • ✅ Validasi input data
  • ✅ Error handling yang baik
  • ✅ Pencarian dan filtering
  • ✅ Pagination untuk data banyak
  • ✅ Testing dengan Postman

Next Steps untuk Belajar Lebih Lanjut:

  1. Authentication dengan Laravel Sanctum
  2. Authorization dan permissions
  3. File upload via API
  4. API documentation dengan Swagger
  5. Unit testing dengan PHPUnit
  6. Deploy ke server production

Useful Commands:

# Development php artisan serve # Jalankan server php artisan migrate # Jalankan migration php artisan db:seed # Isi data sample php artisan tinker # Laravel console php artisan make:model # Buat model baru php artisan make:controller # Buat controller baru # Debugging php artisan route:list # Lihat semua routes php artisan config:clear # Clear cache config

API Anda sekarang siap digunakan dan bisa diintegrasikan dengan aplikasi frontend (React, Vue, Mobile App, dll). Selamat belajar! 🚀