First, install Laravel if you haven't already, using Composer.
Example: Installing Laravel
composer create-project --prefer-dist laravel/laravel api_project
In Laravel, API routes are defined in the routes/api.php
file. These routes are automatically prefixed with /api
.
Example: Defining API Routes
use App\Http\Controllers\Api\PostController;
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']);
index
: To retrieve all posts.show
: To retrieve a specific post.store
: To create a new post.update
: To update an existing post.destroy
: To delete a post.Next, create a controller for handling the logic of your API endpoints. You can use the Artisan command to generate it.
Example: Generating Controller
php artisan make:controller Api/PostController
In the controller, implement the methods to handle the CRUD operations for your API.
Example: Implementing Controller Methods
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use App\Models\Post;
use Illuminate\Http\Request;
class PostController extends Controller
{
// Get all posts
public function index()
{
return response()->json(Post::all());
}
// Get a single post
public function show($id)
{
$post = Post::find($id);
if ($post) {
return response()->json($post);
}
return response()->json(['message' => 'Post not found'], 404);
}
// Store a new post
public function store(Request $request)
{
$validated = $request->validate([
'title' => 'required|string',
'content' => 'required|string',
]);
$post = Post::create($validated);
return response()->json($post, 201);
}
// Update an existing post
public function update(Request $request, $id)
{
$post = Post::find($id);
if (!$post) {
return response()->json(['message' => 'Post not found'], 404);
}
$validated = $request->validate([
'title' => 'required|string',
'content' => 'required|string',
]);
$post->update($validated);
return response()->json($post);
}
// Delete a post
public function destroy($id)
{
$post = Post::find($id);
if ($post) {
$post->delete();
return response()->json(['message' => 'Post deleted successfully']);
}
return response()->json(['message' => 'Post not found'], 404);
}
}
Create a model and migration for the Post
to interact with the database.
Example: Generating Model and Migration
php artisan make:model Post -m
Once the migration is ready, run it to create the necessary database table.
Example: Running Migration
php artisan migrate
Now that the API routes and controllers are set up, you can test the API using tools like Postman or cURL.
Example: Testing with Postman
/api/posts
- Retrieves all posts./api/posts/{id}
- Retrieves a specific post./api/posts
- Creates a new post (provide title
and content
in the body)./api/posts/{id}
- Updates an existing post (provide new title
and content
)./api/posts/{id}
- Deletes a post.Laravel provides a simple, elegant, and powerful way to create RESTful APIs with its built-in tools like controllers, routes, and Eloquent ORM. By following the steps above, you can easily set up and manage CRUD operations in your API, allowing your application to handle client requests seamlessly.